
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
        <link rel="prev" href="../../Kernel_Porting/Porting_Manual_for_GCC/">
      
      
        <link rel="next" href="../API_Reference/">
      
      <link rel="icon" href="../../../assets/favicon.png">
      <meta name="generator" content="mkdocs-1.4.3, mkdocs-material-9.1.19">
    
    
      
        <title>TobudOS内核开发指南 - TobudOS Documentation</title>
      
    
    
      <link rel="stylesheet" href="../../../assets/stylesheets/main.eebd395e.min.css">
      
        
        <link rel="stylesheet" href="../../../assets/stylesheets/palette.ecc896b0.min.css">
      
      

    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="indigo">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#tobudos" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="页眉">
    <a href="../../" title="TobudOS Documentation" class="md-header__button md-logo" aria-label="TobudOS Documentation" data-md-component="logo">
      
  <img src="../../../assets/logo.png" alt="logo">

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            TobudOS Documentation
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              TobudOS内核开发指南
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="indigo"  aria-label="Switch to dark mode"  type="radio" name="__palette" id="__palette_1">
            
              <label class="md-header__button md-icon" title="Switch to dark mode" for="__palette_2" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12 20 8.69Z"/></svg>
              </label>
            
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="indigo" data-md-color-accent="indigo"  aria-label="Switch to light mode"  type="radio" name="__palette" id="__palette_2">
            
              <label class="md-header__button md-icon" title="Switch to light mode" for="__palette_1" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12c0-2.42-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12 20 8.69Z"/></svg>
              </label>
            
          
        </form>
      
    
    
      <div class="md-header__option">
        <div class="md-select">
          
          <button class="md-header__button md-icon" aria-label="选择当前语言">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m12.87 15.07-2.54-2.51.03-.03A17.52 17.52 0 0 0 14.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7 1.62-4.33L19.12 17h-3.24Z"/></svg>
          </button>
          <div class="md-select__inner">
            <ul class="md-select__list">
              
                <li class="md-select__item">
                  <a href="../../../DevelopGuide/Development_Manual/" hreflang="zh" class="md-select__link">
                    中文
                  </a>
                </li>
              
                <li class="md-select__item">
                  <a href="../../../en/DevelopGuide/Development_Manual/" hreflang="en" class="md-select__link">
                    English
                  </a>
                </li>
              
            </ul>
          </div>
        </div>
      </div>
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="查找">
        
          <a href="javascript:void(0)" class="md-search__icon md-icon" title="分享" aria-label="分享" data-clipboard data-clipboard-text="" data-md-component="search-share" tabindex="-1">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M18 16.08c-.76 0-1.44.3-1.96.77L8.91 12.7c.05-.23.09-.46.09-.7 0-.24-.04-.47-.09-.7l7.05-4.11c.54.5 1.25.81 2.04.81a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3c0 .24.04.47.09.7L8.04 9.81C7.5 9.31 6.79 9 6 9a3 3 0 0 0-3 3 3 3 0 0 0 3 3c.79 0 1.5-.31 2.04-.81l7.12 4.15c-.05.21-.08.43-.08.66 0 1.61 1.31 2.91 2.92 2.91 1.61 0 2.92-1.3 2.92-2.91A2.92 2.92 0 0 0 18 16.08Z"/></svg>
          </a>
        
        <button type="reset" class="md-search__icon md-icon" title="清空当前内容" aria-label="清空当前内容" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
        <div class="md-search__suggest" data-md-component="search-suggest"></div>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list" role="presentation"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
      <div class="md-header__source">
        <a href="https://atomgit.com/tobudos/Document" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
  </div>
  <div class="md-source__repository">
    Atomgit
  </div>
</a>
      </div>
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
            
<nav class="md-tabs" aria-label="标签" data-md-component="tabs">
  <div class="md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  <li class="md-tabs__item">
    <a href="../../" class="md-tabs__link">
      首页
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../Introduction/TobudOS_Brief_Introduction/" class="md-tabs__link">
      项目特性简介
    </a>
  </li>

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../Kernel_Porting/Porting_Manual_for_KEIL/" class="md-tabs__link">
        内核移植指南
      </a>
    </li>
  

      
        
  
  
    
  


  
  
  
    <li class="md-tabs__item">
      <a href="./" class="md-tabs__link md-tabs__link--active">
        内核开发指南
      </a>
    </li>
  

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../AT_Framwork_Guide/AT_Firmware_and_SAL_Firmware_User_Guide/" class="md-tabs__link">
        AT模组框架
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../ElfLoader_Guide/ElfLoader_Quick_Start/" class="md-tabs__link">
      ElfLoader动态加载
    </a>
  </li>

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../OTA_Guide/OTA_Quick_Start_based_EVB_MX_Plus/" class="md-tabs__link">
        固件升级
      </a>
    </li>
  

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../MicroPython_Guide/BearPi_MicroPython_QuickStart/" class="md-tabs__link">
      MicroPython开发指南
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../VFS_Guide/vfs/" class="md-tabs__link">
      文件系统
    </a>
  </li>

      
        
  
  


  <li class="md-tabs__item">
    <a href="../../GUI_Guide/gui/" class="md-tabs__link">
      GUI开发
    </a>
  </li>

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../DevelopKit/1.EVB_MX_Plus_Quick_Start/" class="md-tabs__link">
        官方开发套件
      </a>
    </li>
  

      
    </ul>
  </div>
</nav>
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


<nav class="md-nav md-nav--primary md-nav--lifted" aria-label="导航栏" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../../" title="TobudOS Documentation" class="md-nav__button md-logo" aria-label="TobudOS Documentation" data-md-component="logo">
      
  <img src="../../../assets/logo.png" alt="logo">

    </a>
    TobudOS Documentation
  </label>
  
    <div class="md-nav__source">
      <a href="https://atomgit.com/tobudos/Document" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
  </div>
  <div class="md-source__repository">
    Atomgit
  </div>
</a>
    </div>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../" class="md-nav__link">
        首页
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../Introduction/TobudOS_Brief_Introduction/" class="md-nav__link">
        项目特性简介
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_3" >
      
      
        
          
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_3" id="__nav_3_label" tabindex="0">
          内核移植指南
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_3">
          <span class="md-nav__icon md-icon"></span>
          内核移植指南
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../Kernel_Porting/Porting_Manual_for_KEIL/" class="md-nav__link">
        基于Keil移植TobudOS内核
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../Kernel_Porting/Porting_Manual_for_IAR/" class="md-nav__link">
        基于IAR 移植TobudOS内核
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../Kernel_Porting/Porting_Manual_for_GCC/" class="md-nav__link">
        基于GCC 移植TobudOS内核
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_4" checked>
      
      
        
          
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_4" id="__nav_4_label" tabindex="0">
          内核开发指南
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="true">
        <label class="md-nav__title" for="__nav_4">
          <span class="md-nav__icon md-icon"></span>
          内核开发指南
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          TobudOS内核开发指南
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        TobudOS内核开发指南
      </a>
      
        

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    1. 概述
  </a>
  
    <nav class="md-nav" aria-label="1. 概述">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 基础内核组件
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    2. 基础内核
  </a>
  
    <nav class="md-nav" aria-label="2. 基础内核">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#21" class="md-nav__link">
    2.1 系统管理
  </a>
  
    <nav class="md-nav" aria-label="2.1 系统管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#22" class="md-nav__link">
    2.2 任务管理
  </a>
  
    <nav class="md-nav" aria-label="2.2 任务管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_1" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#23" class="md-nav__link">
    2.3 任务间通信
  </a>
  
    <nav class="md-nav" aria-label="2.3 任务间通信">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#231" class="md-nav__link">
    2.3.1 互斥量
  </a>
  
    <nav class="md-nav" aria-label="2.3.1 互斥量">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_2" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_9" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#232" class="md-nav__link">
    2.3.2 信号量
  </a>
  
    <nav class="md-nav" aria-label="2.3.2 信号量">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_10" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_3" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_11" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_12" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#233" class="md-nav__link">
    2.3.3 事件
  </a>
  
    <nav class="md-nav" aria-label="2.3.3 事件">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_13" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_4" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_14" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_15" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#234" class="md-nav__link">
    2.3.4 完成量
  </a>
  
    <nav class="md-nav" aria-label="2.3.4 完成量">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_16" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_5" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_17" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_18" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#235" class="md-nav__link">
    2.3.5 计数锁
  </a>
  
    <nav class="md-nav" aria-label="2.3.5 计数锁">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_19" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_6" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_20" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_21" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#236" class="md-nav__link">
    2.3.6 栅栏
  </a>
  
    <nav class="md-nav" aria-label="2.3.6 栅栏">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_22" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_7" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_23" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_24" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#237" class="md-nav__link">
    2.3.7 消息队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.7 消息队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_25" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_8" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_26" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_27" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#238" class="md-nav__link">
    2.3.8 邮箱队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.8 邮箱队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_28" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_9" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_29" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_30" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#239" class="md-nav__link">
    2.3.9 优先级消息队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.9 优先级消息队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_31" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_10" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_32" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_33" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2310" class="md-nav__link">
    2.3.10 优先级邮箱队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.10 优先级邮箱队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_34" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_11" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_35" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_36" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#24" class="md-nav__link">
    2.4 内存管理
  </a>
  
    <nav class="md-nav" aria-label="2.4 内存管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#241" class="md-nav__link">
    2.4.1 动态内存
  </a>
  
    <nav class="md-nav" aria-label="2.4.1 动态内存">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_37" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_12" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_38" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_39" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#242" class="md-nav__link">
    2.4.2 静态内存
  </a>
  
    <nav class="md-nav" aria-label="2.4.2 静态内存">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_40" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_13" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_41" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_42" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#25" class="md-nav__link">
    2.5 时间管理
  </a>
  
    <nav class="md-nav" aria-label="2.5 时间管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_43" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_14" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_44" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_45" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#26" class="md-nav__link">
    2.6 软件定时器
  </a>
  
    <nav class="md-nav" aria-label="2.6 软件定时器">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_46" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_15" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_47" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_48" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#27" class="md-nav__link">
    2.7 时间片轮转机制
  </a>
  
    <nav class="md-nav" aria-label="2.7 时间片轮转机制">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_49" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_16" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_50" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_51" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#28" class="md-nav__link">
    2.8 内核基础组件
  </a>
  
    <nav class="md-nav" aria-label="2.8 内核基础组件">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#281" class="md-nav__link">
    2.8.1 环形队列
  </a>
  
    <nav class="md-nav" aria-label="2.8.1 环形队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_52" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_17" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_53" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_54" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#282" class="md-nav__link">
    2.8.2 字符流先入先出队列
  </a>
  
    <nav class="md-nav" aria-label="2.8.2 字符流先入先出队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_55" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_18" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_56" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_57" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#283" class="md-nav__link">
    2.8.3 二项堆
  </a>
  
    <nav class="md-nav" aria-label="2.8.3 二项堆">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_58" class="md-nav__link">
    概述
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#284" class="md-nav__link">
    2.8.4 优先级队列
  </a>
  
    <nav class="md-nav" aria-label="2.8.4 优先级队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_59" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_19" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_60" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_61" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#29" class="md-nav__link">
    2.9 功耗管理
  </a>
  
    <nav class="md-nav" aria-label="2.9 功耗管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#291" class="md-nav__link">
    2.9.1 低功耗
  </a>
  
    <nav class="md-nav" aria-label="2.9.1 低功耗">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_62" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_20" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_63" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_64" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#292-tickless" class="md-nav__link">
    2.9.2 tickless
  </a>
  
    <nav class="md-nav" aria-label="2.9.2 tickless">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_65" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_21" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_66" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_67" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../API_Reference/" class="md-nav__link">
        TobudOS内核API参考手册
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../CMSIS_RTOS_API_Use_Guide/" class="md-nav__link">
        TobudOS内核CMSIS适配API指南
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_5" >
      
      
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_5" id="__nav_5_label" tabindex="0">
          AT模组框架
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_5">
          <span class="md-nav__icon md-icon"></span>
          AT模组框架
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../AT_Framwork_Guide/AT_Firmware_and_SAL_Firmware_User_Guide/" class="md-nav__link">
        无线通信模组AT框架开发指南
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../AT_Framwork_Guide/How_To_Use_Esp8266_Tencent_Firmware_Connect_Iot_Explorer/" class="md-nav__link">
        基于AT框架和定制模组如何对接腾讯云IoT Explorer平台
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../ElfLoader_Guide/ElfLoader_Quick_Start/" class="md-nav__link">
        ElfLoader动态加载
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_7" >
      
      
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_7" id="__nav_7_label" tabindex="0">
          固件升级
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_7">
          <span class="md-nav__icon md-icon"></span>
          固件升级
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../OTA_Guide/OTA_Quick_Start_based_EVB_MX_Plus/" class="md-nav__link">
        基于EVB_MX的固件升级指南
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../OTA_Guide/In_Application_Programming_based_EVB_MX_Plus/" class="md-nav__link">
        基于EVB_MX的IAP开发指南
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../MicroPython_Guide/BearPi_MicroPython_QuickStart/" class="md-nav__link">
        MicroPython开发指南
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../VFS_Guide/vfs/" class="md-nav__link">
        文件系统
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../GUI_Guide/gui/" class="md-nav__link">
        GUI开发
      </a>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
      <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_11" >
      
      
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
      
      
        <label class="md-nav__link" for="__nav_11" id="__nav_11_label" tabindex="0">
          官方开发套件
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_11_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_11">
          <span class="md-nav__icon md-icon"></span>
          官方开发套件
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/1.EVB_MX_Plus_Quick_Start/" class="md-nav__link">
        EVB_MX+（STM32L4）快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/2.EVB_LX_RISC-V_Quick_Start/" class="md-nav__link">
        EVB_LX（GD32V RISC-V）快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/3.EVB_WL_Quick_Start/" class="md-nav__link">
        EVB_WL（STM32WL）快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/4.EVB_G0_QuickStart/" class="md-nav__link">
        EVB_G0（STM32G0）快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/5.EVB_AIoT_NXP_QuickStart/" class="md-nav__link">
        EVB_AIoT_NXP（RT1062）快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/6.EVB_CH32V_QuickStart/" class="md-nav__link">
        EVB_CH32V（CH32V307）快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../DevelopKit/7.EVB_AIoT_STM32_QuickStart/" class="md-nav__link">
        EVB_AIoT_STM32（STM32H7）快速上手
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    1. 概述
  </a>
  
    <nav class="md-nav" aria-label="1. 概述">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 基础内核组件
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    2. 基础内核
  </a>
  
    <nav class="md-nav" aria-label="2. 基础内核">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#21" class="md-nav__link">
    2.1 系统管理
  </a>
  
    <nav class="md-nav" aria-label="2.1 系统管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#22" class="md-nav__link">
    2.2 任务管理
  </a>
  
    <nav class="md-nav" aria-label="2.2 任务管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_1" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#23" class="md-nav__link">
    2.3 任务间通信
  </a>
  
    <nav class="md-nav" aria-label="2.3 任务间通信">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#231" class="md-nav__link">
    2.3.1 互斥量
  </a>
  
    <nav class="md-nav" aria-label="2.3.1 互斥量">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_2" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_9" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#232" class="md-nav__link">
    2.3.2 信号量
  </a>
  
    <nav class="md-nav" aria-label="2.3.2 信号量">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_10" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_3" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_11" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_12" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#233" class="md-nav__link">
    2.3.3 事件
  </a>
  
    <nav class="md-nav" aria-label="2.3.3 事件">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_13" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_4" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_14" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_15" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#234" class="md-nav__link">
    2.3.4 完成量
  </a>
  
    <nav class="md-nav" aria-label="2.3.4 完成量">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_16" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_5" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_17" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_18" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#235" class="md-nav__link">
    2.3.5 计数锁
  </a>
  
    <nav class="md-nav" aria-label="2.3.5 计数锁">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_19" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_6" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_20" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_21" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#236" class="md-nav__link">
    2.3.6 栅栏
  </a>
  
    <nav class="md-nav" aria-label="2.3.6 栅栏">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_22" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_7" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_23" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_24" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#237" class="md-nav__link">
    2.3.7 消息队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.7 消息队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_25" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_8" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_26" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_27" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#238" class="md-nav__link">
    2.3.8 邮箱队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.8 邮箱队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_28" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_9" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_29" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_30" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#239" class="md-nav__link">
    2.3.9 优先级消息队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.9 优先级消息队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_31" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_10" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_32" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_33" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2310" class="md-nav__link">
    2.3.10 优先级邮箱队列
  </a>
  
    <nav class="md-nav" aria-label="2.3.10 优先级邮箱队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_34" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_11" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_35" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_36" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#24" class="md-nav__link">
    2.4 内存管理
  </a>
  
    <nav class="md-nav" aria-label="2.4 内存管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#241" class="md-nav__link">
    2.4.1 动态内存
  </a>
  
    <nav class="md-nav" aria-label="2.4.1 动态内存">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_37" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_12" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_38" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_39" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#242" class="md-nav__link">
    2.4.2 静态内存
  </a>
  
    <nav class="md-nav" aria-label="2.4.2 静态内存">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_40" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_13" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_41" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_42" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#25" class="md-nav__link">
    2.5 时间管理
  </a>
  
    <nav class="md-nav" aria-label="2.5 时间管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_43" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_14" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_44" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_45" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#26" class="md-nav__link">
    2.6 软件定时器
  </a>
  
    <nav class="md-nav" aria-label="2.6 软件定时器">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_46" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_15" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_47" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_48" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#27" class="md-nav__link">
    2.7 时间片轮转机制
  </a>
  
    <nav class="md-nav" aria-label="2.7 时间片轮转机制">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_49" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_16" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_50" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_51" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#28" class="md-nav__link">
    2.8 内核基础组件
  </a>
  
    <nav class="md-nav" aria-label="2.8 内核基础组件">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#281" class="md-nav__link">
    2.8.1 环形队列
  </a>
  
    <nav class="md-nav" aria-label="2.8.1 环形队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_52" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_17" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_53" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_54" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#282" class="md-nav__link">
    2.8.2 字符流先入先出队列
  </a>
  
    <nav class="md-nav" aria-label="2.8.2 字符流先入先出队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_55" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_18" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_56" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_57" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#283" class="md-nav__link">
    2.8.3 二项堆
  </a>
  
    <nav class="md-nav" aria-label="2.8.3 二项堆">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_58" class="md-nav__link">
    概述
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#284" class="md-nav__link">
    2.8.4 优先级队列
  </a>
  
    <nav class="md-nav" aria-label="2.8.4 优先级队列">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_59" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_19" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_60" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_61" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#29" class="md-nav__link">
    2.9 功耗管理
  </a>
  
    <nav class="md-nav" aria-label="2.9 功耗管理">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#291" class="md-nav__link">
    2.9.1 低功耗
  </a>
  
    <nav class="md-nav" aria-label="2.9.1 低功耗">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_62" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_20" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_63" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_64" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#292-tickless" class="md-nav__link">
    2.9.2 tickless
  </a>
  
    <nav class="md-nav" aria-label="2.9.2 tickless">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_65" class="md-nav__link">
    概述
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#api_21" class="md-nav__link">
    API 讲解
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_66" class="md-nav__link">
    编程实例
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_67" class="md-nav__link">
    运行效果
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  

  
    <a href="https://atomgit.com/tobudos/Document/edit/master/docs/DevelopGuide/Development_Manual.md" title="编辑此页" class="md-content__button md-icon">
      
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20.71 7.04c.39-.39.39-1.04 0-1.41l-2.34-2.34c-.37-.39-1.02-.39-1.41 0l-1.84 1.83 3.75 3.75M3 17.25V21h3.75L17.81 9.93l-3.75-3.75L3 17.25Z"/></svg>
    </a>
  
  
    
      
    
    <a href="https://atomgit.com/tobudos/Document/raw/master/docs/DevelopGuide/Development_Manual.md" title="查看本页的源代码" class="md-content__button md-icon">
      
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 9a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0 8a5 5 0 0 1-5-5 5 5 0 0 1 5-5 5 5 0 0 1 5 5 5 5 0 0 1-5 5m0-12.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5Z"/></svg>
    </a>
  


<h1 id="tobudos">TobudOS内核开发指南</h1>
<p>目录</p>
<ul>
<li><a href="#TobudOS开发指南">TobudOS内核开发指南</a></li>
<li><a href="#1-概述">1. 概述</a><ul>
<li><a href="#11-基础内核组件">1.1 基础内核组件</a></li>
</ul>
</li>
<li><a href="#2-基础内核">2. 基础内核</a><ul>
<li><a href="#21-系统管理">2.1 系统管理</a></li>
<li><a href="#概述">概述</a></li>
<li><a href="#api讲解">API 讲解</a></li>
<li><a href="#编程实例">编程实例</a></li>
<li><a href="#运行效果">运行效果</a></li>
<li><a href="#22-任务管理">2.2 任务管理</a></li>
<li><a href="#概述-1">概述</a></li>
<li><a href="#api讲解-1">API 讲解</a></li>
<li><a href="#编程实例-1">编程实例</a></li>
<li><a href="#运行效果-1">运行效果</a></li>
<li><a href="#23-任务间通信">2.3 任务间通信</a></li>
<li><a href="#231-互斥量">2.3.1 互斥量</a><ul>
<li><a href="#概述-2">概述</a></li>
<li><a href="#api讲解-2">API 讲解</a></li>
<li><a href="#编程实例-2">编程实例</a></li>
<li><a href="#运行效果-2">运行效果</a></li>
</ul>
</li>
<li><a href="#232-信号量">2.3.2 信号量</a><ul>
<li><a href="#概述-3">概述</a></li>
<li><a href="#api讲解-3">API 讲解</a></li>
<li><a href="#编程实例-3">编程实例</a></li>
<li><a href="#运行效果-3">运行效果</a></li>
</ul>
</li>
<li><a href="#233-事件">2.3.3 事件</a><ul>
<li><a href="#概述-4">概述</a></li>
<li><a href="#api讲解-4">API 讲解</a></li>
<li><a href="#编程实例-4">编程实例</a></li>
<li><a href="#运行效果-4">运行效果</a></li>
</ul>
</li>
<li><a href="#234-完成量">2.3.4 完成量</a><ul>
<li><a href="#概述-5">概述</a></li>
<li><a href="#api讲解-5">API 讲解</a></li>
<li><a href="#编程实例-5">编程实例</a></li>
<li><a href="#运行效果-5">运行效果</a></li>
</ul>
</li>
<li><a href="#235-计数锁">2.3.5 计数锁</a><ul>
<li><a href="#概述-6">概述</a></li>
<li><a href="#api讲解-6">API 讲解</a></li>
<li><a href="#编程实例-6">编程实例</a></li>
<li><a href="#运行效果-6">运行效果</a></li>
</ul>
</li>
<li><a href="#236-栅栏">2.3.6 栅栏</a><ul>
<li><a href="#概述-7">概述</a></li>
<li><a href="#api讲解-7">API 讲解</a></li>
<li><a href="#编程实例-7">编程实例</a></li>
<li><a href="#运行效果-7">运行效果</a></li>
</ul>
</li>
<li><a href="#237-消息队列">2.3.7 消息队列</a><ul>
<li><a href="#概述-8">概述</a></li>
<li><a href="#api讲解-8">API 讲解</a></li>
<li><a href="#编程实例-8">编程实例</a></li>
<li><a href="#运行效果-8">运行效果</a></li>
</ul>
</li>
<li><a href="#238-邮箱队列">2.3.8 邮箱队列</a><ul>
<li><a href="#概述-9">概述</a></li>
<li><a href="#api讲解-9">API 讲解</a></li>
<li><a href="#编程实例-9">编程实例</a></li>
<li><a href="#运行效果-9">运行效果</a></li>
</ul>
</li>
<li><a href="#239-优先级消息队列">2.3.9 优先级消息队列</a><ul>
<li><a href="#概述-10">概述</a></li>
<li><a href="#api讲解-10">API 讲解</a></li>
<li><a href="#编程实例-10">编程实例</a></li>
<li><a href="#运行效果-10">运行效果</a></li>
</ul>
</li>
<li><a href="#2310-优先级邮箱队列">2.3.10 优先级邮箱队列</a><ul>
<li><a href="#概述-11">概述</a></li>
<li><a href="#api讲解-11">API 讲解</a></li>
<li><a href="#编程实例-11">编程实例</a></li>
<li><a href="#运行效果-11">运行效果</a></li>
</ul>
</li>
<li><a href="#24-内存管理">2.4 内存管理</a></li>
<li><a href="#241-动态内存">2.4.1 动态内存</a><ul>
<li><a href="#概述-12">概述</a></li>
<li><a href="#api讲解-12">API 讲解</a></li>
<li><a href="#编程实例-12">编程实例</a></li>
<li><a href="#运行效果-12">运行效果</a></li>
</ul>
</li>
<li><a href="#242-静态内存">2.4.2 静态内存</a><ul>
<li><a href="#概述-13">概述</a></li>
<li><a href="#api讲解-13">API 讲解</a></li>
<li><a href="#编程实例-13">编程实例</a></li>
<li><a href="#运行效果-13">运行效果</a></li>
</ul>
</li>
<li><a href="#25-时间管理">2.5 时间管理</a></li>
<li><a href="#概述-14">概述</a></li>
<li><a href="#api讲解-14">API 讲解</a></li>
<li><a href="#编程实例-14">编程实例</a></li>
<li><a href="#运行效果-14">运行效果</a></li>
<li><a href="#26-软件定时器">2.6 软件定时器</a></li>
<li><a href="#概述-15">概述</a></li>
<li><a href="#api讲解-15">API 讲解</a></li>
<li><a href="#编程实例-15">编程实例</a></li>
<li><a href="#运行效果-15">运行效果</a></li>
<li><a href="#27-时间片轮转机制">2.7 时间片轮转机制</a></li>
<li><a href="#概述-16">概述</a></li>
<li><a href="#api讲解-16">API 讲解</a></li>
<li><a href="#编程实例-16">编程实例</a></li>
<li><a href="#运行效果-16">运行效果</a></li>
<li><a href="#28-内核基础组件">2.8 内核基础组件</a></li>
<li><a href="#281-环形队列">2.8.1 环形队列</a><ul>
<li><a href="#概述-17">概述</a></li>
<li><a href="#api讲解-17">API 讲解</a></li>
<li><a href="#编程实例-17">编程实例</a></li>
<li><a href="#运行效果-17">运行效果</a></li>
</ul>
</li>
<li><a href="#282-字符流先入先出队列">2.8.2 字符流先入先出队列</a><ul>
<li><a href="#概述-18">概述</a></li>
<li><a href="#api讲解-18">API 讲解</a></li>
<li><a href="#编程实例-18">编程实例</a></li>
<li><a href="#运行效果-18">运行效果</a></li>
</ul>
</li>
<li><a href="#283-二项堆">2.8.3 二项堆</a><ul>
<li><a href="#概述-19">概述</a></li>
</ul>
</li>
<li><a href="#284-优先级队列">2.8.4 优先级队列</a><ul>
<li><a href="#概述-20">概述</a></li>
<li><a href="#api讲解-19">API 讲解</a></li>
<li><a href="#编程实例-19">编程实例</a></li>
<li><a href="#运行效果-19">运行效果</a></li>
</ul>
</li>
<li><a href="#29-功耗管理">2.9 功耗管理</a></li>
<li><a href="#291-低功耗">2.9.1 低功耗</a><ul>
<li><a href="#概述-21">概述</a></li>
<li><a href="#api讲解-20">API 讲解</a></li>
<li><a href="#编程实例-20">编程实例</a></li>
<li><a href="#运行效果-20">运行效果</a></li>
</ul>
</li>
<li><a href="#292-tickless">2.9.2 tickless</a><ul>
<li><a href="#概述-22">概述</a></li>
<li><a href="#api讲解-21">API 讲解</a></li>
<li><a href="#编程实例-21">编程实例</a></li>
<li><a href="#运行效果-21">运行效果</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="1">1. 概述</h2>
<p>TobudOS是面向物联网（IOT）领域的操作系统，由一个实现精简的实时操作系统（RTOS）内核，以及丰富的物联网组件组成。</p>
<h3 id="11">1.1 基础内核组件</h3>
<ul>
<li><strong>系统管理</strong></li>
</ul>
<p>系统管理模块，主要提供了内核的初始化、内核运行启动，中断进入/退出流程托管、系统调度锁定及解锁等功能。</p>
<ul>
<li><strong>任务管理</strong></li>
</ul>
<p>提供了任务的创建、删除、睡眠、取消睡眠、挂起、恢复、优先级修改、主动放弃 CPU 等功能。</p>
<ul>
<li><strong>任务间通信</strong></li>
</ul>
<p>提供互斥量、信号量、队列、事件等常用任务间通信机制。</p>
<ul>
<li><strong>内存管理</strong></li>
</ul>
<p>提供了基于堆的动态内存管理，以及静态内存块管理机制。</p>
<ul>
<li><strong>时间管理</strong></li>
</ul>
<p>提供了获取/设置系统时钟滴答数、系统时钟滴答数与墙上时钟时间转换、基于墙上时钟时间的任务睡眠等机制。</p>
<ul>
<li><strong>软件定时器</strong></li>
</ul>
<p>提供了软件定时器的创建、删除、启动、停止等机制。</p>
<ul>
<li><strong>时间片轮转机制</strong></li>
</ul>
<p>TobudOS 内核在提供可抢占式调度内核基础上，还提供了按时间片轮转的 robin 机制。</p>
<ul>
<li><strong>内核基础组件</strong></li>
</ul>
<p>提供了消息队列、字符流先入先出队列等机制。</p>
<ul>
<li><strong>功耗管理</strong></li>
</ul>
<p>提供了 CPU 低功耗运行模式设置、低功耗设备注册、板级唤醒闹钟设置等机制。</p>
<h2 id="2">2. 基础内核</h2>
<h3 id="21">2.1 系统管理</h3>
<h4 id="_1">概述</h4>
<p>系统管理模块提供了几个接口，用以初始化/启动 TobudOS 内核、锁定/解锁系统调度等。</p>
<h4 id="api">API 讲解</h4>
<p><code>k_err_t tos_knl_init(void);</code></p>
<p>初始化内核。</p>
<p><code>k_err_t tos_knl_start(void);</code></p>
<p>启动运行内核，开始第一个任务调度。</p>
<p><code>int tos_knl_is_running(void);</code></p>
<p>判断内核是否已启动运行。</p>
<p><code>void tos_knl_irq_enter(void);</code></p>
<p>此函数应该在中断调用函数的最前端被调用。</p>
<p><code>void tos_knl_irq_leave(void);</code></p>
<p>此函数应该在中断调用函数的尾端被调用。</p>
<p><code>k_err_t tos_knl_sched_lock(void);</code></p>
<p>锁定系统调度，此函数被调用并返回 K_ERR_NONE 时，系统调度会被锁定，系统调度器不再进行任务的切换。</p>
<p><code>k_err_t tos_knl_sched_unlock(void);</code></p>
<p>解锁系统调度，允许任务切换。</p>
<h4 id="_2">编程实例</h4>
<h4 id="_3">运行效果</h4>
<h3 id="22">2.2 任务管理</h3>
<h4 id="_4">概述</h4>
<p>TobudOS内核是单地址空间的可抢占式实时内核，TobudOS 内核不提供进程模型，任务对应线程的概念，是最小的调度运行体，也是最小的资源持有单位。</p>
<p>任务的本质是一个拥有独立栈空间的可调度运行实体，用户可以在任务的入口函数中编写自己的业务逻辑；多个任务之间可以通过系统提供的任务间通信机制进行同步或者信息传递等操作；每个任务都有优先级，高优先级任务可以抢占低优先级任务的运行。</p>
<h4 id="api_1">API 讲解</h4>
<p>创建任务的系统 api 接口为 tos_task_create，接口原型如下：</p>
<div class="highlight"><pre><span></span><code><span class="n">k_err_t</span><span class="w"> </span><span class="nf">tos_task_create</span><span class="p">(</span><span class="n">k_task_t</span><span class="w"> </span><span class="o">*</span><span class="n">task</span><span class="p">,</span>
<span class="w">                        </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">name</span><span class="p">,</span>
<span class="w">                        </span><span class="n">k_task_entry_t</span><span class="w"> </span><span class="n">entry</span><span class="p">,</span>
<span class="w">                        </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">,</span>
<span class="w">                        </span><span class="n">k_prio_t</span><span class="w"> </span><span class="n">prio</span><span class="p">,</span>
<span class="w">                        </span><span class="n">k_stack_t</span><span class="w"> </span><span class="o">*</span><span class="n">stk_base</span><span class="p">,</span>
<span class="w">                        </span><span class="kt">size_t</span><span class="w"> </span><span class="n">stk_size</span><span class="p">,</span>
<span class="w">                        </span><span class="n">k_timeslice_t</span><span class="w"> </span><span class="n">timeslice</span><span class="p">);</span>
</code></pre></div>
<p>这里详细讲解此 api 参数意义：</p>
<ul>
<li>task</li>
</ul>
<p>这是一个 k_task_t 类型的指针，k_task_t 是内核的任务结构体类型。注意：task 指针，应该指向生命周期大于待创建任务体生命周期的 k_task_t 类型变量，如果该指针指向的变量生命周期比待创建的任务体生命周期短，譬如可能是一个生命周期极端的函数栈上变量，可能会出现任务体还在运行而 k_task_t 变量已被销毁，会导致系统调度出现不可预知问题。</p>
<ul>
<li>name</li>
</ul>
<p>指向任务名字符串的指针。注意：同 task，该指针指向的字符串生命周期应该大于待创建的任务体生命周期，一般来说，传入字符串常量指针即可。</p>
<ul>
<li>entry</li>
</ul>
<p>任务体运行的函数入口。当任务创建完毕进入运行状态后，entry 是任务执行的入口，用户可以在此函数中编写业务逻辑。</p>
<ul>
<li>arg</li>
</ul>
<p>传递给任务入口函数的参数。</p>
<ul>
<li>prio</li>
</ul>
<p>任务优先级。prio 的数值越小，优先级越高。用户可以在 tos_config.h 中，通过 TOS_CFG_TASK_PRIO_MAX 来配置任务优先级的最大数值，在内核的实现中，idle 任务的优先级会被分配为 TOS_CFG_TASK_PRIO_MAX - 1，此优先级只能被 idle 任务使用。因此对于一个用户创建的任务来说，合理的优先级范围应该为[0, TOS_CFG_TASK_PRIO_MAX - 2]。另外 TOS_CFG_TASK_PRIO_MAX 的配置值必需大于等于 8。</p>
<ul>
<li>stk_base</li>
</ul>
<p>任务在运行时使用的栈空间的起始地址。注意：同 task，该指针指向的内存空间的生命周期应该大于待创建的任务体生命周期。stk_base 是 k_stack_t 类型的数组起始地址。</p>
<ul>
<li>stk_size</li>
</ul>
<p>任务的栈空间大小。注意：因为 stk_base 是 k_stack_t 类型的数组指针，因此实际栈空间所占内存大小为 stk_size * sizeof(k_stack_t)。</p>
<ul>
<li>timeslice</li>
</ul>
<p>时间片轮转机制下当前任务的时间片大小。当 timeslice 为 0 时，任务调度时间片会被设置为默认大小（TOS_CFG_CPU_TICK_PER_SECOND / 10），系统时钟滴答（systick）数 / 10。</p>
<h4 id="_5">编程实例</h4>
<p>1、在 tos_config.h 中，配置最大任务优先级 TOS_CFG_TASK_PRIO_MAX：</p>
<p><code>#define TOS_CFG_TASK_PRIO_MAX           10u</code></p>
<p>2、配置每秒钟的系统滴答数 TOS_CFG_CPU_TICK_PER_SECOND：</p>
<p><code>#define TOS_CFG_CPU_TICK_PER_SECOND     1000u</code></p>
<p>3、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span><span class="c1">      // 添加 TobudOS 内核接口头文件</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span><span class="c1">   // 包含 mcu 初始化头文件，里面有 board_init 等板级启动代码函数原型声明</span>

<span class="cp">#define STK_SIZE_TASK_PRIO4      512    </span><span class="c1">// 优先级为 4 的任务栈大小为 512</span>
<span class="cp">#define STK_SIZE_TASK_PRIO5      1024   </span><span class="c1">// 优先级为 5 的任务栈大小为 1024</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_prio4</span><span class="p">[</span><span class="n">STK_SIZE_TASK_PRIO4</span><span class="p">];</span><span class="w">    </span><span class="c1">// 优先级为 4 的任务栈空间</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_prio5</span><span class="p">[</span><span class="n">STK_SIZE_TASK_PRIO5</span><span class="p">];</span><span class="w">    </span><span class="c1">// 优先级为 5 的任务栈空间</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_prio4</span><span class="p">;</span><span class="w">    </span><span class="c1">// 优先级为 4 的任务体</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_prio5</span><span class="p">;</span><span class="w">    </span><span class="c1">// 优先级为 5 的任务体</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_prio4</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span><span class="w">    </span><span class="c1">// 优先级为 4 的任务体入口函数</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_prio5</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span><span class="w">    </span><span class="c1">// 优先级为 5 的任务体入口函数</span>

<span class="kt">uint32_t</span><span class="w"> </span><span class="n">arg_task_prio4_array</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w">    </span><span class="c1">// 优先级为 4 的任务体入口函数入参</span>
<span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span>
<span class="p">};</span>

<span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">arg_task_prio5_string</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;arg for task_prio5&quot;</span><span class="p">;</span><span class="w"> </span><span class="c1">// 优先级为 5 的任务体入口函数入参</span>

<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">dump_uint32_array</span><span class="p">(</span><span class="kt">uint32_t</span><span class="w"> </span><span class="o">*</span><span class="n">array</span><span class="p">,</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">len</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">len</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d</span><span class="se">\t</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">);</span>

<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_prio4</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">uint32_t</span><span class="w"> </span><span class="o">*</span><span class="n">array_from_main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">uint32_t</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">arg</span><span class="p">;</span><span class="w">    </span><span class="c1">// 捕获调用者传入的参数</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;array from main:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">dump_uint32_array</span><span class="p">(</span><span class="n">array_from_main</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span><span class="w">  </span><span class="c1">// dump 传入的参数（数组）</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;task_prio4 body</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w">    </span><span class="c1">// 任务运行体，不断打印这条信息</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span><span class="w">   </span><span class="c1">// 睡眠 1000 个系统时钟滴答（以下记作 systick），因为 TOS_CFG_CPU_TICK_PER_SECOND 为 1000，也就是一秒钟会有 1000 个 systick，因此睡眠 1000 个 systick 就是睡眠了 1 秒。</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_prio5</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">string_from_main</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">arg</span><span class="p">;</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;string from main:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%s</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">string_from_main</span><span class="p">);</span><span class="w"> </span><span class="c1">// 打印出调用者传入的字符串参数</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;i = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span><span class="w">  </span><span class="c1">// i 为 2 时，挂起 task_prio4，task_prio4 停止运行</span>
<span class="w">            </span><span class="n">tos_task_suspend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_prio4</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;i = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span><span class="w">  </span><span class="c1">// i 为 4 时，恢复 task_prio4 的运行</span>
<span class="w">            </span><span class="n">tos_task_resume</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_prio4</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;i = %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span><span class="w">  </span><span class="c1">// i 为 6 时，删除 task_prio4，task_prio4 不再运行</span>
<span class="w">            </span><span class="n">tos_task_destroy</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_prio4</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;task_prio5 body</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span><span class="w">   </span><span class="c1">// 执行板级初始化代码，初始化串口等外设。</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span><span class="w"> </span><span class="c1">// 初始化 TobudOS 内核</span>
<span class="w">    </span><span class="c1">// 创建一个优先级为 4 的任务</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_prio4</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;task_prio4&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_prio4</span><span class="p">,</span>
<span class="w">                          </span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="p">)(</span><span class="o">&amp;</span><span class="n">arg_task_prio4_array</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span>
<span class="w">                          </span><span class="n">stack_task_prio4</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_PRIO4</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 创建一个优先级为 5 的任务</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_prio5</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;task_prio5&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_prio5</span><span class="p">,</span>
<span class="w">                          </span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">arg_task_prio5_string</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span>
<span class="w">                          </span><span class="n">stack_task_prio5</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_PRIO5</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 开始内核调度</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="_6">运行效果</h4>
<blockquote>
<p>array from main:
1 2 3</p>
<p>task_prio4 body
string from main:
arg for task_prio5</p>
<p>task_prio5 body
task_prio4 body
task_prio5 body
task_prio4 body
i = 2
task_prio5 body
task_prio5 body
i = 4
task_prio4 body
task_prio5 body
task_prio4 body
task_prio5 body
task_prio4 body
i = 6
task_prio5 body
task_prio5 body
task_prio5 body
task_prio5 body
task_prio5 body
task_prio5 body
task_prio5 body</p>
</blockquote>
<p><a href="../code/2.2%20task%20manager/main.c">实例代码</a></p>
<h3 id="23">2.3 任务间通信</h3>
<h4 id="231">2.3.1 互斥量</h4>
<h5 id="_7">概述</h5>
<p>互斥量又称互斥锁，一般用于共享资源的互斥排他性访问保护。</p>
<p>互斥量在任意时刻处于且仅会处于解锁或锁定状态，当一个任务获取到一把锁后（互斥量锁定），其他任务再尝试获得这把锁时会失败或进入阻塞状态，当该任务释放持有的锁时（互斥量解锁），会唤醒一个正阻塞等待此互斥量的任务，被唤醒的任务将会获取这把锁。</p>
<p>在多任务运行环境中，有些共享资源不具有多线程可重入性，对于这类不希望被多任务同时访问的资源（临界资源），可以采用互斥量来进行保护，后面的编程实例章节会演示这一编程范式。</p>
<h5 id="api_2">API 讲解</h5>
<h5 id="_8">编程实例</h5>
<p>1、在 tos_config.h 中，配置互斥量组件开关 TOS_CFG_MUTEX_EN：</p>
<p><code>#define TOS_CFG_MUTEX_EN       1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_WRITER        512</span>
<span class="cp">#define STK_SIZE_TASK_READER        512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_writer</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WRITER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_reader</span><span class="p">[</span><span class="n">STK_SIZE_TASK_READER</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_writer</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_reader</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_writer</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_reader</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="n">k_mutex_t</span><span class="w"> </span><span class="n">critical_resource_locker</span><span class="p">;</span>

<span class="c1">// 一片临界区内存</span>
<span class="k">static</span><span class="w"> </span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">critical_resource</span><span class="p">[</span><span class="mi">3</span><span class="p">];</span>

<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">write_critical_resource</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">salt</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="c1">// 此函数每次向共享内存中按递增顺序写入三个无符号整数</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;writting critical resource:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d</span><span class="se">\t</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">salt</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">        </span><span class="n">critical_resource</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">salt</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_writer</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">salt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// 在向临界区写入数据之前，先尝试获取临界区保护锁</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mutex_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">critical_resource_locker</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="c1">// 成功获取锁之后，向临界区写入数据</span>
<span class="w">            </span><span class="n">write_critical_resource</span><span class="p">(</span><span class="n">salt</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 写完数据后，释放互斥锁</span>
<span class="w">            </span><span class="n">tos_mutex_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">critical_resource_locker</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">salt</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">read_critical_resource</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 从临界区读取数据</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;reading critical resource:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d</span><span class="se">\t</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">critical_resource</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_reader</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// 读取临界区数据之前，先尝试获取临界区保护锁</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mutex_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">critical_resource_locker</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="c1">// 成功获取锁之后，从临界区读取数据</span>
<span class="w">            </span><span class="n">read_critical_resource</span><span class="p">();</span>
<span class="w">            </span><span class="c1">// 读取数据完毕后，释放互斥锁</span>
<span class="w">            </span><span class="n">tos_mutex_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">critical_resource_locker</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="c1">// 创建临界区保护互斥锁</span>
<span class="w">    </span><span class="n">tos_mutex_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">critical_resource_locker</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_writer</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;writer&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_writer</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_writer</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WRITER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_reader</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;reader&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_reader</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_reader</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_READER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_9">运行效果</h5>
<blockquote>
<p>writting critical resource:
0 1 2
reading critical resource:
0 1 2
writting critical resource:
1 2 3
reading critical resource:
1 2 3
writting critical resource:
2 3 4
reading critical resource:
2 3 4
writting critical resource:
3 4 5
reading critical resource:
3 4 5
writting critical resource:
4 5 6
reading critical resource:
4 5 6
writting critical resource:
5 6 7
reading critical resource:
5 6 7
writting critical resource:
6 7 8
reading critical resource:
6 7 8
writting critical resource:
7 8 9
reading critical resource:
7 8 9</p>
</blockquote>
<p><a href="../code/2.3.1%20mutex/main.c">实例代码</a></p>
<h4 id="232">2.3.2 信号量</h4>
<h5 id="_10">概述</h5>
<p>信号量是一种实现任务间同步的机制，一般用于多个任务间有限资源竞争访问。</p>
<p>通常来说，一个信号量中持有一个整形数值，用以表示可用资源的数量。当一个信号量的可用资源数量大于 0 时，任务尝试获取该信号量成功，信号量的可用资源数减一；当一个信号量的可用资源数等于 0 时，任务尝试获取该信号量失败或进入阻塞状态。信号量的这一模式，当可用资源数为 1 时，可将其用于资源的互斥访问；或者解决生产者 - 消费者问题中的资源生产 - 消费问题。编程实例章节会演示生产者 - 消费者问题的解决范式。</p>
<h5 id="api_3">API 讲解</h5>
<h5 id="_11">编程实例</h5>
<p>1、在 tos_config.h 中，配置信号量组件开关 TOS_CFG_SEM_EN：</p>
<p><code>#define TOS_CFG_SEM_EN     1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_PRODUCER      512</span>
<span class="cp">#define STK_SIZE_TASK_CONSUMER      512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_producer</span><span class="p">[</span><span class="n">STK_SIZE_TASK_PRODUCER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_consumer</span><span class="p">[</span><span class="n">STK_SIZE_TASK_CONSUMER</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_producer</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_consumer</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_producer</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_consumer</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="n">k_mutex_t</span><span class="w"> </span><span class="n">buffer_locker</span><span class="p">;</span>
<span class="n">k_sem_t</span><span class="w"> </span><span class="n">full</span><span class="p">;</span>
<span class="n">k_sem_t</span><span class="w"> </span><span class="n">empty</span><span class="p">;</span>

<span class="cp">#define RESOURCE_COUNT_MAX      3</span>

<span class="k">struct</span><span class="w"> </span><span class="nc">resource_st</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">cursor</span><span class="p">;</span>
<span class="w">    </span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">RESOURCE_COUNT_MAX</span><span class="p">];</span>
<span class="p">}</span><span class="w"> </span><span class="n">resource</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="mi">0</span><span class="p">}</span><span class="w"> </span><span class="p">};</span>

<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">produce_item</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">salt</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;produce item:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">salt</span><span class="p">);</span>
<span class="w">    </span><span class="n">resource</span><span class="p">.</span><span class="n">buffer</span><span class="p">[</span><span class="n">resource</span><span class="p">.</span><span class="n">cursor</span><span class="o">++</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">salt</span><span class="p">;</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_producer</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">salt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_sem_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">empty</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">continue</span><span class="p">;</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mutex_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">buffer_locker</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">continue</span><span class="p">;</span>
<span class="w">        </span><span class="p">}</span>

<span class="w">        </span><span class="n">produce_item</span><span class="p">(</span><span class="n">salt</span><span class="p">);</span>

<span class="w">        </span><span class="n">tos_mutex_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">buffer_locker</span><span class="p">);</span>
<span class="w">        </span><span class="n">tos_sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">full</span><span class="p">);</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">salt</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">consume_item</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;cosume item:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d</span><span class="se">\t</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">resource</span><span class="p">.</span><span class="n">buffer</span><span class="p">[</span><span class="o">--</span><span class="n">resource</span><span class="p">.</span><span class="n">cursor</span><span class="p">]);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_consumer</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_sem_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">full</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">continue</span><span class="p">;</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">tos_mutex_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">buffer_locker</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">continue</span><span class="p">;</span>
<span class="w">        </span><span class="p">}</span>

<span class="w">        </span><span class="n">consume_item</span><span class="p">();</span>

<span class="w">        </span><span class="n">tos_mutex_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">buffer_locker</span><span class="p">);</span>
<span class="w">        </span><span class="n">tos_sem_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">empty</span><span class="p">);</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_mutex_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">buffer_locker</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_sem_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">full</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_sem_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">empty</span><span class="p">,</span><span class="w"> </span><span class="n">RESOURCE_COUNT_MAX</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_producer</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;producer&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_producer</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_producer</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_PRODUCER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_consumer</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;consumer&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_consumer</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_consumer</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_CONSUMER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_12">运行效果</h5>
<blockquote>
<p>produce iterm:
0
cosume iterm:
0
produce iterm:
1
produce iterm:
2
cosume iterm:
2
produce iterm:
3
produce iterm:
4
cosume iterm:
4
produce iterm:
5
cosume iterm:
5
produce iterm:
6
cosume iterm:
6
produce iterm:
7
cosume iterm:
7
produce iterm:
8
cosume iterm:
8
produce iterm:
9
cosume iterm:
9
produce iterm:
10</p>
</blockquote>
<p><a href="../code/2.3.2%20semaphore/main.c">实例代码</a></p>
<h4 id="233">2.3.3 事件</h4>
<h5 id="_13">概述</h5>
<p>事件提供了一种任务间实现同步和信息传递的机制。一般来说，一个事件中包含了一个旗标，这个旗标的每一位表示一个“事件”。</p>
<p>一个任务可以等待一个或者多个“事件”的发生，其他任务在一定的业务条件下可以通过写入特定“事件”唤醒等待此“事件”的任务，实现一种类似信号的编程范式。</p>
<h5 id="api_4">API 讲解</h5>
<h5 id="_14">编程实例</h5>
<p>1、在 tos_config.h 中，配置事件组件开关 TOS_CFG_EVENT_EN：</p>
<p><code>#define TOS_CFG_EVENT_EN       1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_LISTENER      512</span>
<span class="cp">#define STK_SIZE_TASK_TRIGGER       512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_listener1</span><span class="p">[</span><span class="n">STK_SIZE_TASK_LISTENER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_listener2</span><span class="p">[</span><span class="n">STK_SIZE_TASK_LISTENER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_trigger</span><span class="p">[</span><span class="n">STK_SIZE_TASK_TRIGGER</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_listener1</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_listener2</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_trigger</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_listener1</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_listener2</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_trigger</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="k">const</span><span class="w"> </span><span class="n">k_event_flag_t</span><span class="w"> </span><span class="n">event_eeny</span><span class="w">     </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">k_event_flag_t</span><span class="p">)(</span><span class="mi">1</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="k">const</span><span class="w"> </span><span class="n">k_event_flag_t</span><span class="w"> </span><span class="n">event_meeny</span><span class="w">    </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">k_event_flag_t</span><span class="p">)(</span><span class="mi">1</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="k">const</span><span class="w"> </span><span class="n">k_event_flag_t</span><span class="w"> </span><span class="n">event_miny</span><span class="w">     </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">k_event_flag_t</span><span class="p">)(</span><span class="mi">1</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>
<span class="k">const</span><span class="w"> </span><span class="n">k_event_flag_t</span><span class="w"> </span><span class="n">event_moe</span><span class="w">      </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">k_event_flag_t</span><span class="p">)(</span><span class="mi">1</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>

<span class="n">k_event_t</span><span class="w"> </span><span class="n">event</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_listener1</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_event_flag_t</span><span class="w"> </span><span class="n">flag_match</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// 此任务监听四个事件，因为使用了 TOS_OPT_EVENT_PEND_ALL 选项，因此必须是四个事件同时到达此任务才会被唤醒</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_event_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_eeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_meeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_miny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_moe</span><span class="p">,</span>
<span class="w">                                </span><span class="o">&amp;</span><span class="n">flag_match</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_OPT_EVENT_PEND_ALL</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">TOS_OPT_EVENT_PEND_CLR</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_listener1:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;eeny, meeny, miny, moe, they all come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_listener2</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_event_flag_t</span><span class="w"> </span><span class="n">flag_match</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// 此任务监听四个事件，因为使用了 TOS_OPT_EVENT_PEND_ANY 选项，因此四个事件任意一个到达（包括四个事件同时到达）任务就会被唤醒</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_event_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_eeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_meeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_miny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_moe</span><span class="p">,</span>
<span class="w">                                </span><span class="o">&amp;</span><span class="n">flag_match</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_OPT_EVENT_PEND_ANY</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">TOS_OPT_EVENT_PEND_CLR</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_listener2:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 有事件到达，判断具体是哪个事件</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flag_match</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">event_eeny</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;eeny comes</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flag_match</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">event_meeny</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;meeny comes</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flag_match</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">event_miny</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;miny comes</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flag_match</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">event_moe</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;moe comes</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">flag_match</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="p">(</span><span class="n">event_eeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_meeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_miny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_moe</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;all come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_trigger</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_trigger:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;eeny will come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 发送 eeny 事件，task_listener2 会被唤醒</span>
<span class="w">            </span><span class="n">tos_event_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_eeny</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_trigger:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;meeny will come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 发送 eeny 事件，task_listener2 会被唤醒</span>
<span class="w">            </span><span class="n">tos_event_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_meeny</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_trigger:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;miny will come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 发送 eeny 事件，task_listener2 会被唤醒</span>
<span class="w">            </span><span class="n">tos_event_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_miny</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_trigger:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;moe will come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 发送 eeny 事件，task_listener2 会被唤醒</span>
<span class="w">            </span><span class="n">tos_event_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_moe</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry_task_trigger:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;all will come</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="c1">// 同时发送四个事件，因为 task_listener1 的优先级高于 task_listener2，因此这里 task_listener1 会被唤醒</span>
<span class="w">            </span><span class="n">tos_event_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="n">event_eeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_meeny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_miny</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">event_moe</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_event_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">event</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">k_event_flag_t</span><span class="p">)</span><span class="mi">0u</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 这里 task_listener1 的优先级比 task_listener2 高，因此在 task_trigger 发送所有事件时，task_listener1 会被唤醒</span>
<span class="w">    </span><span class="c1">// 读者可以尝试将 task_listener1 优先级修改为 5（比 task_listener2 低），此设置下，在 task_trigger 发送所有事件时，task_listener2 将会被唤醒。</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_listener1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;listener1&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_listener1</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_listener1</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_LISTENER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_listener2</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;listener2&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_listener2</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_listener2</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_LISTENER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_trigger</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;trigger&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_trigger</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_trigger</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_TRIGGER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_15">运行效果</h5>
<blockquote>
<p>entry_task_trigger:
eeny will come
entry_task_listener2:
eeny comes
entry_task_trigger:
meeny will come
entry_task_listener2:
meeny comes
entry_task_trigger:
miny will come
entry_task_listener2:
miny comes
entry_task_trigger:
moe will come
entry_task_listener2:
moe comes
entry_task_trigger:
all will come
entry_task_listener1:
eeny, meeny, miny, moe, they all come</p>
</blockquote>
<p><a href="../code/2.3.3%20event/main.c">实例代码</a></p>
<h4 id="234">2.3.4 完成量</h4>
<h5 id="_16">概述</h5>
<p>完成量是一种简单的任务间通信机制，用以在任务间同步某一事件是否已“完成”的信息。</p>
<h5 id="api_5">API 讲解</h5>
<h5 id="_17">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 这个例子里创建了两个任务，一个任务 task_wait 等待完成量完成，另一个任务负责触发完成量（让完成量完成）</span>
<span class="cm"> */</span>

<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_WAIT      512</span>
<span class="cp">#define STK_SIZE_TASK_TRIGGER   512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_wait</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WAIT</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_trigger</span><span class="p">[</span><span class="n">STK_SIZE_TASK_TRIGGER</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_wait</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_trigger</span><span class="p">;</span>

<span class="n">k_completion_t</span><span class="w"> </span><span class="n">completion</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_wait</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;wait: I won&#39;t go further until someone do the trigger(make it &#39;complete&#39;)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_completion_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">completion</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;wait: someone has made it complete, so I&#39;m here</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_trigger</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;trigger: I&#39;m the one who make complete, anyone waitting for the complete won&#39;t go further until I do the trigger</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_completion_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">completion</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;trigger: I have done the completion</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_completion_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">completion</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_wait</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;wait&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_wait</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_wait</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WAIT</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_trigger</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;trigger&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_trigger</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_trigger</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_TRIGGER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_18">运行效果</h5>
<blockquote>
<p>wait: I won't go further until someone do the trigger(make it 'complete')
trigger: I'm the one who make complete, anyone waitting for the complete won't go further until I do the trigger
wait: someone make it complete, so I'm here
trigger: I have done the completion</p>
</blockquote>
<p><a href="../code/2.3.4%20completion/main.c">实例代码</a></p>
<h4 id="235">2.3.5 计数锁</h4>
<h5 id="_19">概述</h5>
<p>计数锁提供了一种“计数信息”同步的概念，计数锁创建的时候会指定一个计数值，每当有任务执行 tos_countdownlatch_post 时，该计数锁的计数值减一，直到计数锁的计数值为零时，等待此计数锁的任务才会被唤醒。</p>
<h5 id="api_6">API 讲解</h5>
<h5 id="_20">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 假设有这样的业务场景，共有三个勇士，此三个勇士分头去寻找三个武器的碎片，只有这三个勇士都找到碎片后，法师才能将三个碎片合成为武器。用代码看具体如何使用计数锁来完成这个模型。</span>
<span class="cm"> */</span>

<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_WIZARD    512</span>
<span class="cp">#define STK_SIZE_TASK_WARRIOR   512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_wizard</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WIZARD</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_warrior_0</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_warrior_1</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_warrior_2</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_wizard</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_warrior_0</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_warrior_1</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_warrior_2</span><span class="p">;</span>

<span class="n">k_countdownlatch_t</span><span class="w"> </span><span class="n">countdownlatch</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_warrior_0</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 0: I have done my job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_countdownlatch_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">countdownlatch</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_warrior_1</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 1: I have done my job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_countdownlatch_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">countdownlatch</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_warrior_2</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 2: I have done my job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_countdownlatch_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">countdownlatch</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_wizard</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;wizard: I will set 3 warriors to find the fragments</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_countdownlatch_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">countdownlatch</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_warrior_0</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;warrior_0&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_warrior_0</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                                </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_warrior_0</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_warrior_1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;warrior_1&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_warrior_1</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                                </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_warrior_1</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_warrior_2</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;warrior_2&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_warrior_2</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                                </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_warrior_2</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;wizard: now warriors are on their way, I will wait here until they all done the job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_countdownlatch_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">countdownlatch</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;wizard: the warriors all have done their jobs, let&#39;s make the weapon</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_wizard</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;wizard&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_wizard</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_wizard</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WIZARD</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_21">运行效果</h5>
<blockquote>
<p>wizard: I will set 3 warriors to find the fragments
wizard: now warriors are on their way, I will wait here until they all done the job
warrior 0: I have done my job
warrior 1: I have done my job
warrior 2: I have done my job
wizard: the warriors all have done their jobs, let's make the weapon</p>
</blockquote>
<p><a href="../code/2.3.5%20countdownlatch/main.c">实例代码</a></p>
<h4 id="236">2.3.6 栅栏</h4>
<h5 id="_22">概述</h5>
<p>栅栏提供了一种设置任务阻塞屏障的机制，栅栏创建的时候会指定一个计数值，每当有任务执行 tos_barrier_pend 时，该计数锁的计数值减一，直到计数锁的计数值为零时，所有阻塞在 tos_barrier_pend 点上的任务才可以往下运行。</p>
<h5 id="api_7">API 讲解</h5>
<h5 id="_23">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 假设有这样的业务场景，共有三个勇士，此三个勇士分头去寻找三个武器的碎片，任意勇士找到自己的那块武器碎片时，都在原地等待，直到所有的小伙伴都找到了自己的武器碎片后，才各自采取下一步行动。用代码看具体如何使用栅栏来完成这个模型。</span>
<span class="cm"> */</span>

<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_WARRIOR   512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_warrior_0</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_warrior_1</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_warrior_2</span><span class="p">[</span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_warrior_0</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_warrior_1</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_warrior_2</span><span class="p">;</span>

<span class="n">k_barrier_t</span><span class="w"> </span><span class="n">barrier</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_warrior_0</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 0: I&#39;m searching the fragment</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 0: I have done my job, waitting other buddies done their job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_barrier_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">barrier</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 0: all buddies find their fragment, do my next job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_warrior_1</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 1: I&#39;m searching the fragment</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1500</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 1: I have done my job, waitting other buddies done their job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_barrier_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">barrier</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 1: all buddies find their fragment, do my next job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_warrior_2</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 2: I&#39;m searching the fragment</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 2: I have done my job, waitting other buddies done their job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_barrier_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">barrier</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;warrior 2: all buddies find their fragment, do my next job</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_barrier_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">barrier</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">);</span>

<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_warrior_0</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;warrior_0&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_warrior_0</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                                </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_warrior_0</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_warrior_1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;warrior_1&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_warrior_1</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                                </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_warrior_1</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_warrior_2</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;warrior_2&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_warrior_2</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                                </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_warrior_2</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_WARRIOR</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_24">运行效果</h5>
<blockquote>
<p>warrior 0: I'm searching the fragment
warrior 1: I'm searching the fragment
warrior 2: I'm searching the fragment
warrior 0: I have done my job, waitting other buddies done their job
warrior 1: I have done my job, waitting other buddies done their job
warrior 2: I have done my job, waitting other buddies done their job
warrior 2: all buddies find their fragment, do my next job
warrior 0: all buddies find their fragment, do my next job
warrior 1: all buddies find their fragment, do my next job</p>
</blockquote>
<p><a href="../code/2.3.6%20barrier/main.c">实例代码</a></p>
<h4 id="237">2.3.7 消息队列</h4>
<h5 id="_25">概述</h5>
<p>消息队列提供了任务间传递指针数据的机制，所谓的“消息“就是指针。消息本身如何解析使用，由传递消息的两个任务自行规定，消息队列不对消息本身做任何规定和限制，消息队列仅承担指针数据的传递义务。</p>
<h5 id="api_8">API 讲解</h5>
<h5 id="_26">编程实例</h5>
<p>1、在 tos_config.h 中，配置消息队列组件开关 TOS_CFG_MESSAGE_QUEUE_EN：</p>
<p><code>#define TOS_CFG_MESSAGE_QUEUE_EN       1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 这里演示如何使用消息队列在 sender 和 receiver 任务之间传递消息（一个指针，此案例中这个指针信息指向的是一个字符串）</span>
<span class="cm"> */</span>

<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_RECEIVER      512</span>
<span class="cp">#define STK_SIZE_TASK_SENDER        512</span>

<span class="cp">#define PRIO_TASK_RECEIVER_HIGHER_PRIO      4</span>
<span class="cp">#define PRIO_TASK_RECEIVER_LOWER_PRIO       (PRIO_TASK_RECEIVER_HIGHER_PRIO + 1)</span>

<span class="cp">#define MESSAGE_MAX     10</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_receiver_higher_prio</span><span class="p">[</span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_receiver_lower_prio</span><span class="p">[</span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">[</span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">];</span>

<span class="kt">uint8_t</span><span class="w"> </span><span class="n">msg_pool</span><span class="p">[</span><span class="n">MESSAGE_MAX</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="p">)];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_receiver_higher_prio</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_receiver_lower_prio</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_sender</span><span class="p">;</span>

<span class="n">k_msg_q_t</span><span class="w"> </span><span class="n">msg_q</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_higher_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_lower_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_higher_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">msg_received</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_msg_q_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">msg_received</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;higher: msg incoming[%s]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">msg_received</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_lower_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">msg_received</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_msg_q_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">msg_received</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;lower: msg incoming[%s]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">msg_received</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">msg_to_one_receiver</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;message for one receiver(with highest priority)&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">msg_to_all_receiver</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;message for all receivers&quot;</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to one receiver, and shoud be the highest priority one</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">tos_msg_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_to_one_receiver</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to all recevier</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">tos_msg_q_post_all</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_to_all_receiver</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to one receiver, and shoud be the highest priority one</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">tos_msg_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_to_one_receiver</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to all recevier</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">tos_msg_q_post_all</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_to_all_receiver</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_msg_q_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_pool</span><span class="p">,</span><span class="w"> </span><span class="n">MESSAGE_MAX</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_receiver_higher_prio</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_higher_prio&quot;</span><span class="p">,</span>
<span class="w">                            </span><span class="n">entry_task_receiver_higher_prio</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">PRIO_TASK_RECEIVER_HIGHER_PRIO</span><span class="p">,</span>
<span class="w">                            </span><span class="n">stack_task_receiver_higher_prio</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_receiver_lower_prio</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_lower_prio&quot;</span><span class="p">,</span>
<span class="w">                            </span><span class="n">entry_task_receiver_lower_prio</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">PRIO_TASK_RECEIVER_LOWER_PRIO</span><span class="p">,</span>
<span class="w">                            </span><span class="n">stack_task_receiver_lower_prio</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_sender</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;sender&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_sender</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_27">运行效果</h5>
<blockquote>
<p>sender: send a message to one receiver, and shoud be the highest priority one
higher: msg incoming[message for one receiver(with highest priority)]
sender: send a message to all recevier
higher: msg incoming[message for all receivers]
lower: msg incoming[message for all receivers]
sender: send a message to one receiver, and shoud be the highest priority one
higher: msg incoming[message for one receiver(with highest priority)]
sender: send a message to all recevier
higher: msg incoming[message for all receivers]
lower: msg incoming[message for all receivers]</p>
</blockquote>
<p><a href="../code/2.3.7%20message%20queue/main.c">实例代码</a></p>
<h4 id="238">2.3.8 邮箱队列</h4>
<h5 id="_28">概述</h5>
<p>消息队列传递的是指针，邮箱队列传递的是大片的内存数据。</p>
<h5 id="api_9">API 讲解</h5>
<h5 id="_29">编程实例</h5>
<p>1、在 tos_config.h 中，配置邮箱队列组件开关 TOS_CFG_MAIL_QUEUE_EN：</p>
<p><code>#define TOS_CFG_MAIL_QUEUE_EN      1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 这里演示如何使用邮箱队列在 sender 和 receiver 任务之间传递邮箱（此案例中邮件，也就是邮箱要传递的内存数据为一个 mail_t 类型的结构体，从此案例中可以看出来，邮箱队列相对消息队列来说，可以传递更为复杂的内存块数据）</span>
<span class="cm"> */</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_RECEIVER      512</span>
<span class="cp">#define STK_SIZE_TASK_SENDER        512</span>

<span class="cp">#define PRIO_TASK_RECEIVER_HIGHER_PRIO      4</span>
<span class="cp">#define PRIO_TASK_RECEIVER_LOWER_PRIO       (PRIO_TASK_RECEIVER_HIGHER_PRIO + 1)</span>

<span class="cp">#define MAIL_MAX    10</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_receiver_higher_prio</span><span class="p">[</span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_receiver_lower_prio</span><span class="p">[</span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">[</span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">];</span>

<span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">mail_st</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">char</span><span class="w">   </span><span class="o">*</span><span class="n">message</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w">     </span><span class="n">payload</span><span class="p">;</span>
<span class="p">}</span><span class="w"> </span><span class="n">mail_t</span><span class="p">;</span>

<span class="kt">uint8_t</span><span class="w"> </span><span class="n">mail_pool</span><span class="p">[</span><span class="n">MAIL_MAX</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">)];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_receiver_higher_prio</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_receiver_lower_prio</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_sender</span><span class="p">;</span>

<span class="n">k_mail_q_t</span><span class="w"> </span><span class="n">mail_q</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_higher_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_lower_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_higher_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_t</span><span class="w"> </span><span class="n">mail</span><span class="p">;</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">mail_size</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mail_q_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail_size</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">TOS_ASSERT</span><span class="p">(</span><span class="n">mail_size</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;higher: msg incoming[%s], payload[%d]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver_lower_prio</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_t</span><span class="w"> </span><span class="n">mail</span><span class="p">;</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">mail_size</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mail_q_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail_size</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">TOS_ASSERT</span><span class="p">(</span><span class="n">mail_size</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;lower: msg incoming[%s], payload[%d]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_t</span><span class="w"> </span><span class="n">mail</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a mail to one receiver, and shoud be the highest priority one</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;1st time post&quot;</span><span class="p">;</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">            </span><span class="n">tos_mail_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to all recevier</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;2nd time post&quot;</span><span class="p">;</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<span class="w">            </span><span class="n">tos_mail_q_post_all</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to one receiver, and shoud be the highest priority one</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;3rd time post&quot;</span><span class="p">;</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span>
<span class="w">            </span><span class="n">tos_mail_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: send a message to all recevier</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;4th time post&quot;</span><span class="p">;</span>
<span class="w">            </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span>
<span class="w">            </span><span class="n">tos_mail_q_post_all</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_mail_q_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mail_q</span><span class="p">,</span><span class="w"> </span><span class="n">mail_pool</span><span class="p">,</span><span class="w"> </span><span class="n">MAIL_MAX</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_receiver_higher_prio</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_higher_prio&quot;</span><span class="p">,</span>
<span class="w">                            </span><span class="n">entry_task_receiver_higher_prio</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">PRIO_TASK_RECEIVER_HIGHER_PRIO</span><span class="p">,</span>
<span class="w">                            </span><span class="n">stack_task_receiver_higher_prio</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_receiver_lower_prio</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_lower_prio&quot;</span><span class="p">,</span>
<span class="w">                            </span><span class="n">entry_task_receiver_lower_prio</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">PRIO_TASK_RECEIVER_LOWER_PRIO</span><span class="p">,</span>
<span class="w">                            </span><span class="n">stack_task_receiver_lower_prio</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_sender</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;sender&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_sender</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_30">运行效果</h5>
<blockquote>
<p>sender: send a mail to one receiver, and shoud be the highest priority one
higher: msg incoming[1st time post], payload[1]
sender: send a message to all recevier
higher: msg incoming[2nd time post], payload[2]
lower: msg incoming[2nd time post], payload[2]
sender: send a message to one receiver, and shoud be the highest priority one
higher: msg incoming[3rd time post], payload[3]
sender: send a message to all recevier
higher: msg incoming[4th time post], payload[4]
lower: msg incoming[4th time post], payload[4]</p>
</blockquote>
<p><a href="../code/2.3.8%20mail%20queue/main.c">实例代码</a></p>
<h4 id="239">2.3.9 优先级消息队列</h4>
<h5 id="_31">概述</h5>
<p>优先级消息队列相对消息队列来说，给消息附加了一个优先级的概念，较高优先级的消息会比较低优先级的消息更快地被其他任务收到（本质上，消息队列的底层数据容器是环形队列，优先级消息队列的底层数据容器是优先级队列）。</p>
<h5 id="api_10">API 讲解</h5>
<h5 id="_32">编程实例</h5>
<p>1、在 tos_config.h 中，配置优先级消息队列组件开关 TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN：</p>
<p><code>#define TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN      1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_RECEIVER      512</span>
<span class="cp">#define STK_SIZE_TASK_SENDER        512</span>

<span class="cp">#define MESSAGE_MAX     10</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_receiver</span><span class="p">[</span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">[</span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">];</span>

<span class="kt">uint8_t</span><span class="w"> </span><span class="n">msg_pool</span><span class="p">[</span><span class="n">MESSAGE_MAX</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="p">)];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_receiver</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_sender</span><span class="p">;</span>

<span class="n">k_prio_msg_q_t</span><span class="w"> </span><span class="n">prio_msg_q</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">msg_received</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_prio_msg_q_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_msg_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">msg_received</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;receiver: msg incoming[%s]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">msg_received</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">msg_prio_0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;msg with priority 0&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">msg_prio_1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;msg with priority 1&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">msg_prio_2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;msg with priority 2&quot;</span><span class="p">;</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: post a message with priority 2</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_prio_msg_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_prio_2</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: post a message with priority 1</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_prio_msg_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_prio_1</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: post a message with priority 0</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_prio_msg_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_prio_0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_prio_msg_q_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_msg_q</span><span class="p">,</span><span class="w"> </span><span class="n">msg_pool</span><span class="p">,</span><span class="w"> </span><span class="n">MESSAGE_MAX</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_receiver</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_receiver</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_receiver</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_sender</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;sender&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_sender</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_33">运行效果</h5>
<blockquote>
<p>sender: post a message with priority 2
sender: post a message with priority 1
sender: post a message with priority 0
receiver: msg incoming[msg with priority 0]
receiver: msg incoming[msg with priority 1]
receiver: msg incoming[msg with priority 2]</p>
</blockquote>
<p><a href="../code/2.3.9%20priority%20message%20queue/main.c">实例代码</a></p>
<h4 id="2310">2.3.10 优先级邮箱队列</h4>
<h5 id="_34">概述</h5>
<p>优先级邮箱队列相对邮箱队列来说，给邮件附加了一个优先级的概念，较高优先级的邮件会比较低优先级的邮件更快地被其他任务收到（本质上，邮箱队列的底层数据容器是环形队列，优先级邮箱队列的底层数据容器是优先级队列）。</p>
<h5 id="api_11">API 讲解</h5>
<h5 id="_35">编程实例</h5>
<p>1、在 tos_config.h 中，配置优先级邮箱队列组件开关 TOS_CFG_PRIORITY_MAIL_QUEUE_EN：</p>
<p><code>#define TOS_CFG_PRIORITY_MAIL_QUEUE_EN     1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 这里演示了优先级邮箱队列的使用，从 sender 任务中的逻辑可以看出来，依次 post 了三个 mail，优先级按时间顺序依次为 2、1、0（数值越高优先级越低）。如果是传统的邮箱队列，那个 receiver 应该是依次收到优先级为 2、1、0 的邮件；但这是优先级邮箱队列，因而 receiver 会按优先级顺序收到这三个邮件，也就是依次收到优先级为 0、1、2 的邮件。</span>
<span class="cm"> */</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_RECEIVER      512</span>
<span class="cp">#define STK_SIZE_TASK_SENDER        512</span>

<span class="cp">#define MAIL_MAX    10</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_receiver</span><span class="p">[</span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">[</span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">];</span>

<span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">mail_st</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">char</span><span class="w">   </span><span class="o">*</span><span class="n">message</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w">     </span><span class="n">payload</span><span class="p">;</span>
<span class="p">}</span><span class="w"> </span><span class="n">mail_t</span><span class="p">;</span>

<span class="kt">uint8_t</span><span class="w"> </span><span class="n">mail_pool</span><span class="p">[</span><span class="n">MAIL_MAX</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">)];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_receiver</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_sender</span><span class="p">;</span>

<span class="n">k_prio_mail_q_t</span><span class="w"> </span><span class="n">prio_mail_q</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_receiver</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_t</span><span class="w"> </span><span class="n">mail</span><span class="p">;</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">mail_size</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_prio_mail_q_pend</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail_size</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_TIME_FOREVER</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">TOS_ASSERT</span><span class="p">(</span><span class="n">mail_size</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;receiver: msg incoming[%s], payload[%d]</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">mail</span><span class="p">.</span><span class="n">message</span><span class="p">,</span><span class="w"> </span><span class="n">mail</span><span class="p">.</span><span class="n">payload</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sender</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">mail_t</span><span class="w"> </span><span class="n">mail_0</span><span class="p">,</span><span class="w"> </span><span class="n">mail_1</span><span class="p">,</span><span class="w"> </span><span class="n">mail_2</span><span class="p">;</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: post a mail with priority 2</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">mail_2</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;priority 2&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_2</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span>
<span class="w">    </span><span class="n">tos_prio_mail_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail_2</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">),</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: post a mail with priority 1</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">mail_1</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;priority 1&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_1</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="n">tos_prio_mail_q_post_all</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail_1</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">),</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;sender: post a mail with priority 0</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">mail_0</span><span class="p">.</span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;priority 0&quot;</span><span class="p">;</span>
<span class="w">    </span><span class="n">mail_0</span><span class="p">.</span><span class="n">payload</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="n">tos_prio_mail_q_post</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_mail_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">mail_0</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">),</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_prio_mail_q_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_mail_q</span><span class="p">,</span><span class="w"> </span><span class="n">mail_pool</span><span class="p">,</span><span class="w"> </span><span class="n">MAIL_MAX</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">mail_t</span><span class="p">));</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_receiver</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_receiver</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_receiver</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_RECEIVER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_sender</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;sender&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_sender</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_sender</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_SENDER</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_36">运行效果</h5>
<blockquote>
<p>sender: post a mail with priority 2
sender: post a mail with priority 1
sender: post a mail with priority 0
receiver: msg incoming[priority 0], payload[0]
receiver: msg incoming[priority 1], payload[1]
receiver: msg incoming[priority 2], payload[2]</p>
</blockquote>
<p><a href="../code/2.3.10%20priority%20mail%20queue/main.c">实例代码</a></p>
<h3 id="24">2.4 内存管理</h3>
<h4 id="241">2.4.1 动态内存</h4>
<h5 id="_37">概述</h5>
<p>动态内存管理模块，提供了一套动态管理系统内存的机制，支持用户动态的申请、释放不定长内存块。</p>
<h5 id="api_12">API 讲解</h5>
<h5 id="_38">编程实例</h5>
<p>1、在 tos_config.h 中，配置动态内存组件开关 TOS_CFG_MMHEAP_EN：</p>
<p><code>#define TOS_CFG_MMHEAP_EN      1u</code></p>
<p>2、在 tos_config.h 中，配置动态内存池大小：</p>
<p><code>#define TOS_CFG_MMHEAP_POOL_SIZE   0x2000</code></p>
<p>3、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">K_NULL</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">p_aligned</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">K_NULL</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmheap_alloc</span><span class="p">(</span><span class="mh">0x30</span><span class="p">);</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;alloc: %x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">cpu_addr_t</span><span class="p">)</span><span class="n">p</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;free: %x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">);</span>
<span class="w">                </span><span class="n">tos_mmheap_free</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">3</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmheap_alloc</span><span class="p">(</span><span class="mh">0x30</span><span class="p">);</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;alloc: %x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">cpu_addr_t</span><span class="p">)</span><span class="n">p</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">p_aligned</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmheap_aligned_alloc</span><span class="p">(</span><span class="mh">0x50</span><span class="p">,</span><span class="w"> </span><span class="mi">16</span><span class="p">);</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p_aligned</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;aligned alloc: %x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">cpu_addr_t</span><span class="p">)</span><span class="n">p_aligned</span><span class="p">);</span>
<span class="w">                </span><span class="k">if</span><span class="w"> </span><span class="p">((</span><span class="n">cpu_addr_t</span><span class="p">)</span><span class="n">p_aligned</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">16</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%x is 16 aligned</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">cpu_addr_t</span><span class="p">)</span><span class="n">p_aligned</span><span class="p">);</span>
<span class="w">                </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">                    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should not happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">                </span><span class="p">}</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmheap_realloc</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="mh">0x40</span><span class="p">);</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;realloc: %x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">cpu_addr_t</span><span class="p">)</span><span class="n">p</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">6</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">tos_mmheap_free</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">p_aligned</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="n">tos_mmheap_free</span><span class="p">(</span><span class="n">p_aligned</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span>

<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">        </span><span class="o">++</span><span class="n">i</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_higher_prio&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_39">运行效果</h5>
<blockquote>
<p>alloc: 20000c8c
free: 20000c8c
alloc: 20000c8c
aligned alloc: 20000cc0
20000cc0 is 16 aligned
realloc: 20000d14</p>
</blockquote>
<p><a href="../code/2.4.1%20mmheap/main.c">实例代码</a></p>
<h4 id="242">2.4.2 静态内存</h4>
<h5 id="_40">概述</h5>
<p>静态内存管理模块，提供了一套管理静态内存块的机制，支持用户申请、释放定长的内存块。</p>
<h5 id="api_13">API 讲解</h5>
<p>创建静态内存池接口：</p>
<div class="highlight"><pre><span></span><code><span class="n">k_err_t</span><span class="w"> </span><span class="nf">tos_mmblk_pool_create</span><span class="p">(</span><span class="n">k_mmblk_pool_t</span><span class="w"> </span><span class="o">*</span><span class="n">mbp</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">pool_start</span><span class="p">,</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">blk_num</span><span class="p">,</span><span class="w"> </span><span class="kt">size_t</span><span class="w"> </span><span class="n">blk_size</span><span class="p">);</span>
</code></pre></div>
<p>这里详细讲解此 api 参数意义：</p>
<ul>
<li>mbp</li>
</ul>
<p>静态内存池句柄。</p>
<ul>
<li>pool_start</li>
</ul>
<p>静态内存池起始地址。</p>
<ul>
<li>blk_num</li>
</ul>
<p>内存池将要划分的内存块个数。</p>
<ul>
<li>blk_size</li>
</ul>
<p>每个内存块的大小。</p>
<h5 id="_41">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="cp">#define MMBLK_BLK_NUM       5</span>
<span class="cp">#define MMBLK_BLK_SIZE      0x20</span>

<span class="n">k_mmblk_pool_t</span><span class="w"> </span><span class="n">mmblk_pool</span><span class="p">;</span>

<span class="c1">// 需要管理的静态内存池</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">mmblk_pool_buffer</span><span class="p">[</span><span class="n">MMBLK_BLK_NUM</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">MMBLK_BLK_SIZE</span><span class="p">];</span>

<span class="c1">// 记录从内存池中分配到的地址</span>
<span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">p</span><span class="p">[</span><span class="n">MMBLK_BLK_NUM</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">K_NULL</span><span class="w"> </span><span class="p">};</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">p_dummy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">K_NULL</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;mmblk_pool has %d blocks, size of each block is 0x%x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mh">0x20</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 从内存池中获取所有的 block</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">MMBLK_BLK_NUM</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmblk_alloc</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mmblk_pool</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d block alloced: 0x%x</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should not happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// 前文逻辑已经将所有可用 block 分配完毕，继续分配会返回 K_ERR_MMBLK_POOL_EMPTY 错误码</span>
<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmblk_alloc</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mmblk_pool</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">p_dummy</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_MMBLK_POOL_EMPTY</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;blocks exhausted, all blocks is alloced</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should not happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// 将前文分配得到的所有 block 归还到池中</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">MMBLK_BLK_NUM</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmblk_free</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mmblk_pool</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should not happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="c1">// 前文的归还动作中已经将所有的 block 归还到池中，继续规范会返回 K_ERR_MMBLK_POOL_FULL 错误码</span>
<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_mmblk_free</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mmblk_pool</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_MMBLK_POOL_FULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;pool is full</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should not happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="c1">// 创建静态内存池</span>
<span class="w">    </span><span class="n">tos_mmblk_pool_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">mmblk_pool</span><span class="p">,</span><span class="w"> </span><span class="n">mmblk_pool_buffer</span><span class="p">,</span><span class="w"> </span><span class="n">MMBLK_BLK_NUM</span><span class="p">,</span><span class="w"> </span><span class="n">MMBLK_BLK_SIZE</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_higher_prio&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_42">运行效果</h5>
<blockquote>
<p>mmblk_pool has 5 blocks, size of each block is 0x20
0 block alloced: 0x20000974
1 block alloced: 0x20000994
2 block alloced: 0x200009b4
3 block alloced: 0x200009d4
4 block alloced: 0x200009f4
blocks exhausted, all blocks is alloced
pool is full</p>
</blockquote>
<p><a href="../code/2.4.2%20mmblk/main.c">实例代码</a></p>
<h3 id="25">2.5 时间管理</h3>
<h4 id="_43">概述</h4>
<p>时间管理，提供了一族与时间相关的函数，可以获取/设置系统时钟滴答数（systick）、systick 与毫秒单位之间互相转化、按毫秒、墙上时钟等单位进行任务睡眠的功能。</p>
<h4 id="api_14">API 讲解</h4>
<h4 id="_44">编程实例</h4>
<p>1、配置每秒钟的系统滴答数 TOS_CFG_CPU_TICK_PER_SECOND：</p>
<p><code>#define TOS_CFG_CPU_TICK_PER_SECOND     1000u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_time_t</span><span class="w"> </span><span class="n">ms</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_tick_t</span><span class="w"> </span><span class="n">systick</span><span class="p">,</span><span class="w"> </span><span class="n">after_systick</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 因为 TOS_CFG_CPU_TICK_PER_SECOND 为 1000，也就是一秒钟会有 1000 个 systick，因此 1000 个 systick 等于 1000 毫秒。</span>
<span class="w">    </span><span class="n">systick</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_millisec2tick</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d millisec equals to %lld ticks</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">2000</span><span class="p">,</span><span class="w"> </span><span class="n">systick</span><span class="p">);</span>

<span class="w">    </span><span class="n">ms</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_tick2millisec</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%lld ticks equals to %d millisec</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">k_tick_t</span><span class="p">)</span><span class="mi">1000</span><span class="p">,</span><span class="w"> </span><span class="n">ms</span><span class="p">);</span>

<span class="w">    </span><span class="n">systick</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">();</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;before sleep, systick is %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">systick</span><span class="p">);</span>

<span class="w">    </span><span class="n">tos_msleep</span><span class="p">(</span><span class="mi">2000</span><span class="p">);</span>

<span class="w">    </span><span class="n">after_systick</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">();</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;after sleep %d ms, systick is %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">2000</span><span class="p">,</span><span class="w"> </span><span class="n">after_systick</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;milliseconds sleep is about: %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_ticks2millisec</span><span class="p">(</span><span class="n">after_systick</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">systick</span><span class="p">));</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_higher_prio&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="_45">运行效果</h4>
<blockquote>
<p>2000 millisec equals to 2000 ticks
1000 ticks equals to 1000 millisec
before sleep, systick is 7
after sleep 2000 ms, systick is 2009
milliseconds sleep is about: 2002</p>
</blockquote>
<p><a href="../code/2.5%20time/main.c">实例代码</a></p>
<h3 id="26">2.6 软件定时器</h3>
<h4 id="_46">概述</h4>
<p>软件定时器提供了一套从软件层次实现的定时器机制，相对应的概念是硬件定时器。用户可以创建一系列的软件定时器，并指定软件定时器到期的条件以及执行回调，当软件定时器到期时会执行注册的回调。</p>
<p>通常来说，用户注册的软件定时器回调中很可能包含延迟动作或同步等待操作，或者回调函数本身逻辑复杂执行耗时较长，因此系统将软件定时器管理逻辑设计成一个任务，在这个任务中扫描定时器是否过期并执行定时器回调。但是如你所知，创建一个任务是需要消耗系统内存资源的（任务的栈、任务句柄本身的内存空间等等），而如果用户注册的软件定时器回调中并不包含延迟动作也不包含同步等待操作，或者回调本身执行耗时很短，这种情况下软件定时器管理逻辑无需被设计成任务，而是可以被设计成时钟中断中被调用的一个函数。当软件定时器管理逻辑被设计成一个函数时，就可以节省创建任务所需的资源。</p>
<p>系统默认采用的实现是将定时器管理逻辑设计为任务，当用户的定时器回调都是耗时极短的操作时，用户可以通过将软件定时器管理逻辑配置为函数来节省内存资源。通过在 tos_config.h 中打开 TOS_CFG_TIMER_AS_PROC 开关来讲软件定时器管理逻辑配置为函数：</p>
<p><code>#define TOS_CFG_TIMER_AS_PROC      1u</code></p>
<h4 id="api_15">API 讲解</h4>
<div class="highlight"><pre><span></span><code><span class="n">k_err_t</span><span class="w"> </span><span class="n">tos_timer_create</span><span class="p">(</span><span class="n">k_timer_t</span><span class="w"> </span><span class="o">*</span><span class="n">tmr</span><span class="p">,</span>
<span class="w">                         </span><span class="n">k_tick_t</span><span class="w"> </span><span class="n">delay</span><span class="p">,</span>
<span class="w">                         </span><span class="n">k_tick_t</span><span class="w"> </span><span class="n">period</span><span class="p">,</span>
<span class="w">                         </span><span class="n">k_timer_callback_t</span><span class="w"> </span><span class="n">callback</span><span class="p">,</span>
<span class="w">                         </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">cb_arg</span><span class="p">,</span>
<span class="w">                         </span><span class="n">k_opt_t</span><span class="w"> </span><span class="n">opt</span><span class="p">)</span>
</code></pre></div>
<p>这里详细讲解此 api 参数意义：</p>
<ul>
<li>tmr</li>
</ul>
<p>软件定时器句柄。</p>
<ul>
<li>delay</li>
</ul>
<p>该定时器延迟多久后执行。</p>
<ul>
<li>period</li>
</ul>
<p>一个定时器的执行周期。</p>
<ul>
<li>callback</li>
</ul>
<p>定时器到期后的执行回调。</p>
<ul>
<li>cb_arg</li>
</ul>
<p>执行回调的入参。</p>
<ul>
<li>opt</li>
</ul>
<p>此 opt 的传入主要是界定 tmr 的属性，如果传入的是 TOS_OPT_TIMER_ONESHOT，表明此 tmr 是一次性的，当 delay 时间到期，tmr 的执行回调调用完毕后此 tmr 生命周期就结束了；如果传入的是 TOS_OPT_TIMER_PERIODIC，表明此 tmr 是周期性定时器，当 tmr 定时时间到期，tmr 的执行回调调用完毕后，系统会重新按 period 参数为到期时间将 tmr 加入到定时队列，开启下一个周期。</p>
<h4 id="_47">编程实例</h4>
<p>1、在 tos_config.h 中，配置软件定时器组件开关 TOS_CFG_TIMER_EN：</p>
<p><code>#define TOS_CFG_TIMER_EN   1000u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">oneshot_timer_cb</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;this is oneshot timer callback, current systick: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">());</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">periodic_timer_cb</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;this is periodic timer callback, current systick: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">());</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_timer_t</span><span class="w"> </span><span class="n">oneshot_tmr</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_timer_t</span><span class="w"> </span><span class="n">periodic_tmr</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 这是一个一次性的 timer，且超时时间是 3000 个 tick 之后</span>
<span class="w">    </span><span class="n">tos_timer_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">oneshot_tmr</span><span class="p">,</span><span class="w"> </span><span class="mi">3000</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">oneshot_timer_cb</span><span class="p">,</span><span class="w"> </span><span class="n">K_NULL</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_OPT_TIMER_ONESHOT</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 这是一个周期性的 timer，第一次超时时间是 2000 个 tick 之后，之后按 3000 个 tick 为周期执行回调</span>
<span class="w">    </span><span class="n">tos_timer_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">periodic_tmr</span><span class="p">,</span><span class="w"> </span><span class="mi">2000</span><span class="p">,</span><span class="w"> </span><span class="mi">3000</span><span class="p">,</span><span class="w"> </span><span class="n">periodic_timer_cb</span><span class="p">,</span><span class="w"> </span><span class="n">K_NULL</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_OPT_TIMER_PERIODIC</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;current systick: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">());</span>
<span class="w">    </span><span class="n">tos_timer_start</span><span class="p">(</span><span class="o">&amp;</span><span class="n">oneshot_tmr</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_timer_start</span><span class="p">(</span><span class="o">&amp;</span><span class="n">periodic_tmr</span><span class="p">);</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;receiver_higher_prio&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="_48">运行效果</h4>
<blockquote>
<p>current systick: 0
this is periodic timer callback, current systick: 2001
this is oneshot timer callback, current systick: 3001
this is periodic timer callback, current systick: 5001
this is periodic timer callback, current systick: 8001
this is periodic timer callback, current systick: 11001
this is periodic timer callback, current systick: 14001</p>
</blockquote>
<p><a href="../code/2.6%20timer/main.c">实例代码</a></p>
<h3 id="27">2.7 时间片轮转机制</h3>
<h4 id="_49">概述</h4>
<p>TobudOS 操作系统内核是一个抢占式内核，抢占式内核的特点是，如果最高优先级的任务不放弃 CPU（调用 tos_task_delay、tos_task_yeild 等主动放权，或者任务间同步通信机制的 pend 接口等），那么 CPU 将会一直被此任务独占。</p>
<p>假设这样一种场景：系统中包含多个同等优先级的任务，且这几个任务体中都没有放弃 CPU 的行为，则会出现的情况是，这几个任务始终只有第一个被得到调度的那个在运行，因为第一个得到调度的任务体中不会主动放弃 CPU，而其他任务优先级上与其相等无法抢占。此种场景下，其他任务会因得不到 CPU 而陷入饥饿状态。</p>
<p>时间片轮转机制提供了按时间片占用调度的策略，可以解决上述场景下的任务饥饿问题。</p>
<h4 id="api_16">API 讲解</h4>
<h4 id="_50">编程实例</h4>
<p>1、在 tos_config.h 中，配置时间片轮转组件开关 TOS_CFG_ROUND_ROBIN_EN：</p>
<p><code>#define TOS_CFG_ROUND_ROBIN_EN     1u</code></p>
<p>2、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>
<span class="cp">#define STK_SIZE_TASK_SAMPLE    512</span>

<span class="cm">/*</span>
<span class="cm">此代码中创建了两个同等优先级（PRIO_TASK_DEMO）的任务 task_demo1、task_demo2，两个任务体中做的操作是不断对各自的计数器（demo1_counter、demo2_counter）做自增操作（没有放弃 CPU 的操作），时间片分别为 timeslice_demo1、timeslice_demo2。</span>
<span class="cm">同时创建了一个优先级比 task_demo1、task_demo2 较高的采样任务 task_sample，此任务不间歇的对两个计数器进行采样。在开启时间片轮转的情况下，task_demo1、task_demo2 得到运行的时间比例应该是 timeslice_demo1 与 timeslice_demo2 的比例，那么 demo1_counter 和 demo2_counter 值的比例应该也差不多是 timeslice_demo1 与 timeslice_demo2 的比例。</span>
<span class="cm">*/</span>

<span class="c1">// task_demo1 和 task_demo2 的优先级</span>
<span class="cp">#define PRIO_TASK_DEMO          4</span>
<span class="c1">// 采样任务的优先级</span>
<span class="cp">#define PRIO_TASK_SAMPLE        (PRIO_TASK_DEMO - 1)</span>

<span class="c1">// task_demo1 的时间片，在 tos_task_create 时传入</span>
<span class="k">const</span><span class="w"> </span><span class="n">k_timeslice_t</span><span class="w"> </span><span class="n">timeslice_demo1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span>
<span class="c1">// task_demo2 的时间片，在 tos_task_create 时传入</span>
<span class="k">const</span><span class="w"> </span><span class="n">k_timeslice_t</span><span class="w"> </span><span class="n">timeslice_demo2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">20</span><span class="p">;</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo1</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo2</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>
<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_sample</span><span class="p">[</span><span class="n">STK_SIZE_TASK_SAMPLE</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo1</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo2</span><span class="p">;</span>
<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_sample</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo1</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo2</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>
<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sample</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">uint64_t</span><span class="w"> </span><span class="n">demo1_counter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="kt">uint64_t</span><span class="w"> </span><span class="n">demo2_counter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo1</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="o">++</span><span class="n">demo1_counter</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo2</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="o">++</span><span class="n">demo2_counter</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_sample</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="o">++</span><span class="n">demo2_counter</span><span class="p">;</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;demo1_counter: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">demo1_counter</span><span class="p">);</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;demo2_counter: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">demo2_counter</span><span class="p">);</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;demo2_counter / demo1_counter = %f</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span>
<span class="w">            </span><span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">demo2_counter</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">demo1_counter</span><span class="p">);</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should almost equals to:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;timeslice_demo2 / timeslice_demo1 = %f</span><span class="se">\n\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">double</span><span class="p">)</span><span class="n">timeslice_demo2</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">timeslice_demo1</span><span class="p">);</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="c1">// 配置 robin 机制参数</span>
<span class="w">    </span><span class="n">tos_robin_default_timeslice_config</span><span class="p">((</span><span class="n">k_timeslice_t</span><span class="p">)</span><span class="mi">500u</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo1</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;demo1&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo1</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo1</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span>
<span class="w">                            </span><span class="n">timeslice_demo1</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo2</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;demo2&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo2</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo2</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span>
<span class="w">                            </span><span class="n">timeslice_demo2</span><span class="p">);</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_sample</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;sample&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_sample</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_SAMPLE</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_sample</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_SAMPLE</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="_51">运行效果</h4>
<blockquote>
<p>demo1_counter: 0
demo2_counter: 1
demo2_counter / demo1_counter = 0.000000
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 1915369
demo2_counter: 3720158
demo2_counter / demo1_counter = 1.942267
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 3774985
demo2_counter: 7493508
demo2_counter / demo1_counter = 1.985043
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 5634601
demo2_counter: 11266858
demo2_counter / demo1_counter = 1.999584
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 7546896
demo2_counter: 14987015
demo2_counter / demo1_counter = 1.985852
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 9406512
demo2_counter: 18759340
demo2_counter / demo1_counter = 1.994293
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 11266128
demo2_counter: 22531664
demo2_counter / demo1_counter = 1.999947
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 13177398
demo2_counter: 26251821
demo2_counter / demo1_counter = 1.992185
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 15037014
demo2_counter: 30023632
demo2_counter / demo1_counter = 1.996649
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 16896630
demo2_counter: 33795443
demo2_counter / demo1_counter = 2.000129
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
<p>demo1_counter: 18807900
demo2_counter: 37515600
demo2_counter / demo1_counter = 1.994672
should almost equals to:
timeslice_demo2 / timeslice_demo1 = 2.000000</p>
</blockquote>
<p><a href="../code/2.7%20robin/main.c">实例代码</a></p>
<h3 id="28">2.8 内核基础组件</h3>
<h4 id="281">2.8.1 环形队列</h4>
<h5 id="_52">概述</h5>
<p>环形队列本质上就是支持先入先出操作的环形 buffer，是系统的一个基础组件，通常用来作为实现上层机制的底层数据容器。</p>
<h5 id="api_17">API 讲解</h5>
<h5 id="_53">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="cp">#define PRIO_TASK_DEMO          4</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">item_st</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="p">}</span><span class="w"> </span><span class="n">item_t</span><span class="p">;</span>

<span class="cp">#define RING_QUEUE_ITEM_MAX        5</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">ring_q_buffer</span><span class="p">[</span><span class="n">RING_QUEUE_ITEM_MAX</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">)];</span>

<span class="n">k_ring_q_t</span><span class="w"> </span><span class="n">rinq_q</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="n">item_t</span><span class="w"> </span><span class="n">item</span><span class="p">;</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">item_size</span><span class="p">;</span>

<span class="w">    </span><span class="n">tos_ring_q_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">,</span><span class="w"> </span><span class="n">ring_q_buffer</span><span class="p">,</span><span class="w"> </span><span class="n">RING_QUEUE_ITEM_MAX</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">));</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">RING_QUEUE_ITEM_MAX</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;enqueue: %d  %d  %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">        </span><span class="n">item</span><span class="p">.</span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">        </span><span class="n">item</span><span class="p">.</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">        </span><span class="n">item</span><span class="p">.</span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_ring_q_enqueue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">));</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_ring_q_enqueue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">));</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_RING_Q_FULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;ring queue is full: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_ring_q_is_full</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">RING_QUEUE_ITEM_MAX</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_ring_q_dequeue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item_size</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;dequeue: %d %d %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">c</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_ring_q_dequeue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item_size</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_RING_Q_EMPTY</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;ring queue is empty: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_ring_q_is_empty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">rinq_q</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;demo&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_54">运行效果</h5>
<blockquote>
<p>enqueue: 0 0 0
enqueue: 1 1 1
enqueue: 2 2 2
enqueue: 3 3 3
enqueue: 4 4 4
ring queue is full: TRUE
dequeue: 0 0 0
dequeue: 1 1 1
dequeue: 2 2 2
dequeue: 3 3 3
dequeue: 4 4 4
ring queue is empty: TRUE</p>
</blockquote>
<p><a href="../code/2.8.1%20ring%20queue/main.c">实例代码</a></p>
<h4 id="282">2.8.2 字符流先入先出队列</h4>
<h5 id="_55">概述</h5>
<p>字符流先入先出队列，提供的是一个面向字符操作的环形队列实现，提供了基本的字符流入队出队操作。本质上就是环形队列中元素为字符（单字节长度）时的特例，实际上字符流先出先出队列底层的实现就是环形队列。</p>
<h5 id="api_18">API 讲解</h5>
<h5 id="_56">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="cp">#define PRIO_TASK_DEMO          4</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="cp">#define FIFO_BUFFER_SIZE        5</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">fifo_buffer</span><span class="p">[</span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">];</span>

<span class="n">k_chr_fifo_t</span><span class="w"> </span><span class="n">fifo</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">char_push</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">data</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 往 fifo 中压入 FIFO_BUFFER_SIZE 个字符，分别是 a、b、c、d、e</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;char pushed: %c</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;a&#39;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_push</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;a&#39;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// fifo 最多包含 FIFO_BUFFER_SIZE 个字符，上文逻辑中已经压入了最大的字符量，此时继续压入字符会返回 K_ERR_FIFO_FULL（fifo 已满）</span>
<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_push</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;z&#39;</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_RING_Q_FULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;fifo is full: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_chr_fifo_is_full</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// 从 fifo 中把所有的字符都弹出来</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_pop</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%d pop: %c</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="c1">// 此时继续弹出字符，会返回 K_ERR_FIFO_EMPTY（fifo 已空）</span>
<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_pop</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_RING_Q_EMPTY</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;fifo is empty: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_chr_fifo_is_empty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">stream_push</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">stream</span><span class="p">[</span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="sc">&#39;a&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;b&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;c&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;d&#39;</span><span class="p">,</span><span class="w"> </span><span class="sc">&#39;e&#39;</span><span class="w"> </span><span class="p">};</span>
<span class="w">    </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">stream_dummy</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="sc">&#39;z&#39;</span><span class="w"> </span><span class="p">};</span>
<span class="w">    </span><span class="kt">uint8_t</span><span class="w"> </span><span class="n">stream_pop</span><span class="p">[</span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">];</span>

<span class="w">    </span><span class="c1">// 压入字符流，字符流的长度是 5，不超过 fifo 的最大长度 FIFO_BUFFER_SIZE，会压入成功，并返回压入字符流的长度 5</span>
<span class="w">    </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_push_stream</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">stream</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">count</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// 继续压入字符流（即使是长度为 1 的字符流），因 fifo 已满无法继续压入，返回长度 0（压入失败）</span>
<span class="w">    </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_push_stream</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">stream_dummy</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">count</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;fifo is full: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_chr_fifo_is_full</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// 将前文中压入的字符流全部弹出，返回前文压入的字符流长度 5（弹出的字符流长度）</span>
<span class="w">    </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_pop_stream</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">stream_pop</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">count</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;stream popped:</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%c&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">stream_pop</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="c1">// 继续弹出，因 fifo 已空，返回 0</span>
<span class="w">    </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_chr_fifo_pop_stream</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">stream_pop</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">count</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;fifo is empty: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_chr_fifo_is_empty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="c1">// 创建了一个最多包含 FIFO_BUFFER_SIZE 个字符的 fifo</span>
<span class="w">    </span><span class="n">tos_chr_fifo_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fifo</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">fifo_buffer</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="w"> </span><span class="n">FIFO_BUFFER_SIZE</span><span class="p">);</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;fifo, dealing with char</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">char_push</span><span class="p">();</span>

<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;fifo, dealing with stream</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="n">stream_push</span><span class="p">();</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;demo&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_57">运行效果</h5>
<blockquote>
<p>fifo, dealing with char
char pushed: a
char pushed: b
char pushed: c
char pushed: d
char pushed: e
fifo is full: TRUE
0 pop: a
1 pop: b
2 pop: c
3 pop: d
4 pop: e
fifo is empty: TRUE
fifo, dealing with stream
fifo is full: TRUE
stream popped:
abcde
fifo is empty: TRUE</p>
</blockquote>
<p><a href="../code/2.8.2%20char%20fifo/main.c">实例代码</a></p>
<h4 id="283">2.8.3 二项堆</h4>
<h5 id="_58">概述</h5>
<p>此组件用来内部实现优先级队列，不推荐用户使用。</p>
<h4 id="284">2.8.4 优先级队列</h4>
<h5 id="_59">概述</h5>
<p>提供了基于优先级的队列管理。环形队列的入队出队规则是先入队的先出队（first in, first out），优先级队列的出队顺序是按照优先级来的，优先级较高的元素先出队。</p>
<h5 id="api_19">API 讲解</h5>
<h5 id="_60">编程实例</h5>
<p>1、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cm">/*</span>
<span class="cm"> 此案例展示了优先级队列的出队规则：依次入队优先级为 5、4、3、2、1 的元素，出队时的顺序是按照优先级来的，也就是按优先级从高到低（数值越大，优先级越小），依次出队优先级为 1、2、3、4、5 元素。</span>
<span class="cm"> */</span>

<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="cp">#define PRIO_TASK_DEMO          4</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">item_st</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">c</span><span class="p">;</span>
<span class="p">}</span><span class="w"> </span><span class="n">item_t</span><span class="p">;</span>

<span class="cp">#define PRIO_QUEUE_ITEM_MAX     5</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">ring_q_buffer</span><span class="p">[</span><span class="n">PRIO_QUEUE_ITEM_MAX</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">)];</span>
<span class="kt">uint8_t</span><span class="w"> </span><span class="n">mgr_pool</span><span class="p">[</span><span class="n">TOS_PRIO_Q_MGR_ARRAY_SIZE</span><span class="p">(</span><span class="n">PRIO_QUEUE_ITEM_MAX</span><span class="p">)];</span>

<span class="n">k_prio_q_t</span><span class="w"> </span><span class="n">prio_q</span><span class="p">;</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_err_t</span><span class="w"> </span><span class="n">err</span><span class="p">;</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w">    </span><span class="n">item_t</span><span class="w"> </span><span class="n">item</span><span class="p">;</span>
<span class="w">    </span><span class="n">k_prio_t</span><span class="w"> </span><span class="n">prio</span><span class="p">;</span>
<span class="w">    </span><span class="kt">size_t</span><span class="w"> </span><span class="n">item_size</span><span class="p">;</span>

<span class="w">    </span><span class="n">tos_prio_q_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">,</span><span class="w"> </span><span class="n">mgr_pool</span><span class="p">,</span><span class="w"> </span><span class="n">ring_q_buffer</span><span class="p">,</span><span class="w"> </span><span class="n">PRIO_QUEUE_ITEM_MAX</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">));</span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PRIO_QUEUE_ITEM_MAX</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="o">--</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;enqueue: %d  %d  %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">        </span><span class="n">item</span><span class="p">.</span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">        </span><span class="n">item</span><span class="p">.</span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">        </span><span class="n">item</span><span class="p">.</span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_prio_q_enqueue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">),</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_prio_q_enqueue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">item_t</span><span class="p">),</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_PRIO_Q_FULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;priority queue is full: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_prio_q_is_full</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">PRIO_QUEUE_ITEM_MAX</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_prio_q_dequeue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item_size</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">prio</span><span class="p">);</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_NONE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;dequeue: %d %d %d, prio: %d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">prio</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="n">err</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tos_prio_q_dequeue</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">item_size</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">prio</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">err</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">K_ERR_PRIO_Q_EMPTY</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;priority queue is empty: %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_prio_q_is_empty</span><span class="p">(</span><span class="o">&amp;</span><span class="n">prio_q</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;TRUE&quot;</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">&quot;FALSE&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;should never happen</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>

<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;demo&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_61">运行效果</h5>
<blockquote>
<p>enqueue: 5 5 5
enqueue: 4 4 4
enqueue: 3 3 3
enqueue: 2 2 2
enqueue: 1 1 1
priority queue is full: TRUE
dequeue: 1 1 1, prio: 1
dequeue: 2 2 2, prio: 2
dequeue: 3 3 3, prio: 3
dequeue: 4 4 4, prio: 4
dequeue: 5 5 5, prio: 5
priority queue is empty: TRUE</p>
</blockquote>
<p><a href="../code/2.8.4%20priority%20queue/main.c">实例代码</a></p>
<h3 id="29">2.9 功耗管理</h3>
<h4 id="291">2.9.1 低功耗</h4>
<h5 id="_62">概述</h5>
<p>TobudOS 提供了多级低功耗管理框架。初级低功耗的方案是，当系统处于“空闲”状态，也即进入 idle 任务时，系统调用处理器（目前支持的架构是 arm v7m）低功耗接口进入短暂的睡眠模式。</p>
<h5 id="api_20">API 讲解</h5>
<h5 id="_63">编程实例</h5>
<p>对于初级低功耗模式，无需用户编写任何代码，直接通过在 tos_config.h 打开 TOS_CFG_PMR_MGR_EN 开关即可：</p>
<p><code>#define TOS_CFG_PWR_MGR_EN     1u</code></p>
<h5 id="_64">运行效果</h5>
<h4 id="292-tickless">2.9.2 tickless</h4>
<h5 id="_65">概述</h5>
<p>TobudOS 的 tickless 机制提供了一套非周期性时钟的方案，在系统无需 systick 驱动调度的情况下，停掉 systick。</p>
<p>初级功耗管理方案下，因为还有系统 systick 的存在，因此系统进入 idle 任务后，并不会在睡眠模式下停留太久。要想进入到更极致的低功耗状态，需要暂停 systick。</p>
<p>arm 架构提供三级低功耗模式，sleep、stop、standby 模式，三种模式运行功耗逐次降低，standby 模式最低。TobudOS 的内核提供了简洁清晰的接口来管理各级模式。</p>
<h5 id="api_21">API 讲解</h5>
<div class="highlight"><pre><span></span><code><span class="kt">void</span><span class="w"> </span><span class="nf">tos_tickless_wkup_alarm_install</span><span class="p">(</span><span class="n">k_cpu_lpwr_mode_t</span><span class="w"> </span><span class="n">mode</span><span class="p">,</span><span class="w"> </span><span class="n">k_tickless_wkup_alarm_t</span><span class="w"> </span><span class="o">*</span><span class="n">wkup_alarm</span><span class="p">);</span>
</code></pre></div>
<p>此接口用以安装各低功耗模式下的唤醒闹钟。当内核进入 tickless 模式下后，systick 以及停止了，因此需要其他计时器来将 CPU 从低功耗模式下唤醒。</p>
<p>根据 arm v7m 的芯片规格，三种模式下的唤醒源分别为：</p>
<ul>
<li>sleep</li>
</ul>
<p>CPU 进入 sleep 模式后，可以由 systick、硬件 timer、RTC 时钟唤醒（wakeup/alarm 中断）。</p>
<ul>
<li>stop</li>
</ul>
<p>CPU 进入 stop 模式后，可以由 RTC 时钟（wakeup/alarm 中断）唤醒。</p>
<ul>
<li>standby</li>
</ul>
<p>CPU 进入 standby 模式后，只可由 RTC 时钟的 alarm 中断唤醒（还可以通过外部管脚唤醒，但这不属于 TobudOS 内核机制设计的范畴）。</p>
<p>k_tickless_wkup_alarm_t 定义如下：</p>
<div class="highlight"><pre><span></span><code><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">k_tickless_wakeup_alarm_st</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w">         </span><span class="p">(</span><span class="o">*</span><span class="n">init</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="w">    </span><span class="kt">int</span><span class="w">         </span><span class="p">(</span><span class="o">*</span><span class="n">setup</span><span class="p">)(</span><span class="n">k_time_t</span><span class="w"> </span><span class="n">millisecond</span><span class="p">);</span>
<span class="w">    </span><span class="kt">int</span><span class="w">         </span><span class="p">(</span><span class="o">*</span><span class="n">dismiss</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span>
<span class="w">    </span><span class="n">k_time_t</span><span class="w">    </span><span class="p">(</span><span class="o">*</span><span class="n">max_delay</span><span class="p">)(</span><span class="kt">void</span><span class="p">);</span><span class="w"> </span><span class="cm">/* in millisecond */</span>
<span class="p">}</span><span class="w"> </span><span class="n">k_tickless_wkup_alarm_t</span><span class="p">;</span>
</code></pre></div>
<p>一个唤醒闹钟有四个成员方法：</p>
<ul>
<li>init</li>
</ul>
<p>闹钟初始化函数。</p>
<ul>
<li>setup</li>
</ul>
<p>闹钟设定函数，入参为闹钟到期时间（单位毫秒）。此闹钟在设定完毕后的 millisecond 毫秒时来中断。</p>
<ul>
<li>dismiss</li>
</ul>
<p>闹钟解除函数，执行完后闹钟中断不会再来。</p>
<ul>
<li>max_delay</li>
</ul>
<p>此闹钟最长的到期时间（单位为毫秒）。</p>
<div class="highlight"><pre><span></span><code><span class="n">k_err_t</span><span class="w"> </span><span class="nf">tos_tickless_wkup_alarm_init</span><span class="p">(</span><span class="n">k_cpu_lpwr_mode_t</span><span class="w"> </span><span class="n">mode</span><span class="p">);</span>
</code></pre></div>
<p>此函数用来初始化特定模式下的唤醒闹钟（实际上调用的是 tos_tickless_wkup_alarm_install 接口中安装的 k_tickless_wkup_alarm_t 的 init 方法）。</p>
<div class="highlight"><pre><span></span><code><span class="n">k_err_t</span><span class="w"> </span><span class="nf">tos_pm_cpu_lpwr_mode_set</span><span class="p">(</span><span class="n">k_cpu_lpwr_mode_t</span><span class="w"> </span><span class="n">cpu_lpwr_mode</span><span class="p">);</span>
</code></pre></div>
<p>设置内核在 tickless 模式下进入的 CPU 低功耗模式。</p>
<h5 id="_66">编程实例</h5>
<p>1、在 tos_config.h 中，配置低功耗组件开关 TOS_CFG_PWR_MGR_EN：</p>
<p><code>#define TOS_CFG_PWR_MGR_EN 1u</code></p>
<p>2、在 tos_config.h 中，配置 tickless 组件开关 TOS_CFG_TICKLESS_EN：</p>
<p><code>#define TOS_CFG_TICKLESS_EN    1u</code></p>
<p>3、编写 main.c 示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;mcu_init.h&quot;</span>

<span class="cp">#define STK_SIZE_TASK_DEMO      512</span>

<span class="cp">#define PRIO_TASK_DEMO          4</span>

<span class="n">k_stack_t</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">[</span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">];</span>

<span class="n">k_task_t</span><span class="w"> </span><span class="n">task_demo</span><span class="p">;</span>

<span class="k">extern</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">);</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">timer_callback</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;timer callback: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">());</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">entry_task_demo</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">k_timer_t</span><span class="w"> </span><span class="n">tmr</span><span class="p">;</span>

<span class="w">    </span><span class="c1">// 创建一个软件定时器，每 6000 个 tick 触发一次</span>
<span class="w">    </span><span class="n">tos_timer_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tmr</span><span class="p">,</span><span class="w"> </span><span class="mi">0u</span><span class="p">,</span><span class="w"> </span><span class="mi">6000u</span><span class="p">,</span><span class="w"> </span><span class="n">timer_callback</span><span class="p">,</span><span class="w"> </span><span class="n">K_NULL</span><span class="p">,</span><span class="w"> </span><span class="n">TOS_OPT_TIMER_PERIODIC</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_timer_start</span><span class="p">(</span><span class="o">&amp;</span><span class="n">tmr</span><span class="p">);</span>

<span class="w">    </span><span class="c1">// 此任务体内每 3000 个 tick 运行一次</span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;entry task demo: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">());</span>
<span class="w">        </span><span class="n">tos_task_delay</span><span class="p">(</span><span class="mi">3000</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">board_init</span><span class="p">();</span>
<span class="w">    </span><span class="n">tos_knl_init</span><span class="p">();</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">tos_task_create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">task_demo</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;demo1&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">entry_task_demo</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span>
<span class="w">                            </span><span class="n">PRIO_TASK_DEMO</span><span class="p">,</span><span class="w"> </span><span class="n">stack_task_demo</span><span class="p">,</span><span class="w"> </span><span class="n">STK_SIZE_TASK_DEMO</span><span class="p">,</span>
<span class="w">                            </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">tos_knl_start</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<p>4、实现 tos_bsp_tickless_setup 回调（参考 board\TOS_tiny_EVK_STM32L431CBT6\BSP\Src\tickless\bsp_pwr_mgr.c、board\TOS_tiny_EVK_STM32L431CBT6\BSP\Src\tickless\bsp_tickless_alarm.c）：</p>
<div class="highlight"><pre><span></span><code><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tos_k.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tickless/bsp_pm_device.h&quot;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;tickless/bsp_tickless_alarm.h&quot;</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">tos_bsp_tickless_setup</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="cp">#if TOS_CFG_TICKLESS_EN &gt; 0u</span>
<span class="w">    </span><span class="c1">// sleep 模式下的唤醒源，基本定时器</span>
<span class="w">    </span><span class="n">tos_tickless_wkup_alarm_install</span><span class="p">(</span><span class="n">TOS_LOW_POWER_MODE_SLEEP</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">tickless_wkup_alarm_tim</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 初始化唤醒源闹钟</span>
<span class="w">    </span><span class="n">tos_tickless_wkup_alarm_init</span><span class="p">(</span><span class="n">TOS_LOW_POWER_MODE_SLEEP</span><span class="p">);</span>
<span class="w">    </span><span class="c1">// 设置 tickless 状态时进入 sleep 模式</span>
<span class="w">    </span><span class="n">tos_pm_cpu_lpwr_mode_set</span><span class="p">(</span><span class="n">TOS_LOW_POWER_MODE_SLEEP</span><span class="p">);</span>
<span class="cp">#endif</span>
<span class="p">}</span>
</code></pre></div>
<p>5、为了观察在 tickless 时是否确实没有 systick 中断，在 tos_sys.c 的 idle 任务体内加一句调试代码：</p>
<div class="highlight"><pre><span></span><code><span class="n">__STATIC__</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">knl_idle_entry</span><span class="p">(</span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">arg</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arg</span><span class="p">;</span><span class="w"> </span><span class="c1">// make compiler happy</span>

<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">K_TRUE</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="c1">// 这里在 idle 任务体内加上一句打印，如果 systick 正常开启，在没有用户任务运行时，此调试信息会不断打印；如果是 tickless 状态，此调试信息应该只会第一次进入 idle 任务时，或在用户任务等待到期，或用户的软件定时器到期时，才打印一次。</span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;idle entry: %lld</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">tos_systick_get</span><span class="p">());</span>
<span class="cp">#if TOS_CFG_PWR_MGR_EN &gt; 0u</span>
<span class="w">        </span><span class="n">pm_power_manager</span><span class="p">();</span>
<span class="cp">#endif</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<h5 id="_67">运行效果</h5>
<blockquote>
<p>entry task demo: 0
idle entry: 2
entry task demo: 3002
idle entry: 3002
timer callback: 6000
idle entry: 6000
entry task demo: 6002
idle entry: 6002
entry task demo: 9002
idle entry: 9002
timer callback: 12000
idle entry: 12000
entry task demo: 12002
idle entry: 12002
entry task demo: 15002
idle entry: 15002
timer callback: 18000
idle entry: 18000
entry task demo: 18002
idle entry: 18002</p>
</blockquote>





                
              </article>
            </div>
          
          
        </div>
        
          <button type="button" class="md-top md-icon" data-md-component="top" hidden>
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12Z"/></svg>
            回到页面顶部
          </button>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
    <div class="md-copyright__highlight">
      Copyright &copy; 2023 TobudOS
    </div>
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "../../..", "features": ["content.action.edit", "content.action.view", "content.code.annotate", "content.tooltips", "navigation.indexes", "navigation.sections", "navigation.tabs", "navigation.top", "navigation.tracking", "search.highlight", "search.share", "search.suggest", "toc.follow"], "search": "../../../assets/javascripts/workers/search.74e28a9f.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
    
    
      <script src="../../../assets/javascripts/bundle.220ee61c.min.js"></script>
      
    
  </body>
</html>